home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / dwarfread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-29  |  84.2 KB  |  3,125 lines

  1. /* DWARF debugging format support for GDB.
  2.    Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  3.    Written by Fred Fish at Cygnus Support.  Portions based on dbxread.c,
  4.    mipsread.c, coffread.c, and dwarfread.c from a Data General SVR4 gdb port.
  5.  
  6. This file is part of GDB.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /*
  23.  
  24. FIXME: Figure out how to get the frame pointer register number in the
  25. execution environment of the target.  Remove R_FP kludge
  26.  
  27. FIXME: Add generation of dependencies list to partial symtab code.
  28.  
  29. FIXME: Currently we ignore host/target byte ordering and integer size
  30. differences.  Should remap data from external form to an internal form
  31. before trying to use it.
  32.  
  33. FIXME: Resolve minor differences between what information we put in the
  34. partial symbol table and what dbxread puts in.  For example, we don't yet
  35. put enum constants there.  And dbxread seems to invent a lot of typedefs
  36. we never see.  Use the new printpsym command to see the partial symbol table
  37. contents.
  38.  
  39. FIXME: Figure out a better way to tell gdb about the name of the function
  40. contain the user's entry point (I.E. main())
  41.  
  42. FIXME: The current DWARF specification has a very strong bias towards
  43. machines with 32-bit integers, as it assumes that many attributes of the
  44. program (such as an address) will fit in such an integer.  There are many
  45. references in the spec to things that are 2, 4, or 8 bytes long.  Given that
  46. we will probably run into problems on machines where some of these assumptions
  47. are invalid (64-bit ints for example), we don't bother at this time to try to
  48. make this code more flexible and just use shorts, ints, and longs (and their
  49. sizes) where it seems appropriate.  I.E. we use a short int to hold DWARF
  50. tags, and assume that the tag size in the file is the same as sizeof(short).
  51.  
  52. FIXME: See other FIXME's and "ifdef 0" scattered throughout the code for
  53. other things to work on, if you get bored. :-)
  54.  
  55. */
  56.  
  57. #include "defs.h"
  58. #include <varargs.h>
  59. #include <fcntl.h>
  60. #include <string.h>
  61.  
  62. #include "bfd.h"
  63. #include "symtab.h"
  64. #include "gdbtypes.h"
  65. #include "symfile.h"
  66. #include "objfiles.h"
  67. #include "libbfd.h"        /* FIXME Secret Internal BFD stuff (bfd_read) */
  68. #include "elf/dwarf.h"
  69. #include "buildsym.h"
  70.  
  71. #ifdef MAINTENANCE    /* Define to 1 to compile in some maintenance stuff */
  72. #define SQUAWK(stuff) dwarfwarn stuff
  73. #else
  74. #define SQUAWK(stuff)
  75. #endif
  76.  
  77. #ifndef R_FP        /* FIXME */
  78. #define R_FP 14        /* Kludge to get frame pointer register number */
  79. #endif
  80.  
  81. typedef unsigned int DIEREF;    /* Reference to a DIE */
  82.  
  83. #ifndef GCC_PRODUCER
  84. #define GCC_PRODUCER "GNU C "
  85. #endif
  86.  
  87. #define STREQ(a,b)        (strcmp(a,b)==0)
  88. #define STREQN(a,b,n)        (strncmp(a,b,n)==0)
  89.  
  90. /* The Amiga SVR4 header file <dwarf.h> defines AT_element_list as a
  91.    FORM_BLOCK2, and this is the value emitted by the AT&T compiler.
  92.    However, the Issue 2 DWARF specification from AT&T defines it as
  93.    a FORM_BLOCK4, as does the latest specification from UI/PLSIG.
  94.    For backwards compatibility with the AT&T compiler produced executables
  95.    we define AT_short_element_list for this variant. */
  96.  
  97. #define    AT_short_element_list     (0x00f0|FORM_BLOCK2)
  98.  
  99. /* External variables referenced. */
  100.  
  101. extern int info_verbose;        /* From main.c; nonzero => verbose */
  102. extern char *warning_pre_print;        /* From utils.c */
  103.  
  104. /* The DWARF debugging information consists of two major pieces,
  105.    one is a block of DWARF Information Entries (DIE's) and the other
  106.    is a line number table.  The "struct dieinfo" structure contains
  107.    the information for a single DIE, the one currently being processed.
  108.  
  109.    In order to make it easier to randomly access the attribute fields
  110.    of the current DIE, which are specifically unordered within the DIE
  111.    each DIE is scanned and an instance of the "struct dieinfo"
  112.    structure is initialized.
  113.  
  114.    Initialization is done in two levels.  The first, done by basicdieinfo(),
  115.    just initializes those fields that are vital to deciding whether or not
  116.    to use this DIE, how to skip past it, etc.  The second, done by the
  117.    function completedieinfo(), fills in the rest of the information.
  118.  
  119.    Attributes which have block forms are not interpreted at the time
  120.    the DIE is scanned, instead we just save pointers to the start
  121.    of their value fields.
  122.  
  123.    Some fields have a flag <name>_p that is set when the value of the
  124.    field is valid (I.E. we found a matching attribute in the DIE).  Since
  125.    we may want to test for the presence of some attributes in the DIE,
  126.    such as AT_low_pc, without restricting the values of the field,
  127.    we need someway to note that we found such an attribute.
  128.    
  129.  */
  130.    
  131. typedef char BLOCK;
  132.  
  133. struct dieinfo {
  134.   char *    die;            /* Pointer to the raw DIE data */
  135.   long        dielength;        /* Length of the raw DIE data */
  136.   DIEREF    dieref;            /* Offset of this DIE */
  137.   short        dietag;            /* Tag for this DIE */
  138.   long        at_padding;
  139.   long        at_sibling;
  140.   BLOCK *    at_location;
  141.   char *    at_name;
  142.   unsigned short at_fund_type;
  143.   BLOCK *    at_mod_fund_type;
  144.   long        at_user_def_type;
  145.   BLOCK *    at_mod_u_d_type;
  146.   short        at_ordering;
  147.   BLOCK *    at_subscr_data;
  148.   long        at_byte_size;
  149.   short        at_bit_offset;
  150.   long        at_bit_size;
  151.   BLOCK *    at_element_list;
  152.   long        at_stmt_list;
  153.   long        at_low_pc;
  154.   long        at_high_pc;
  155.   long        at_language;
  156.   long        at_member;
  157.   long        at_discr;
  158.   BLOCK *    at_discr_value;
  159.   short        at_visibility;
  160.   long        at_import;
  161.   BLOCK *    at_string_length;
  162.   char *    at_comp_dir;
  163.   char *    at_producer;
  164.   long        at_frame_base;
  165.   long        at_start_scope;
  166.   long        at_stride_size;
  167.   long        at_src_info;
  168.   short        at_prototyped;
  169.   unsigned int    has_at_low_pc:1;
  170.   unsigned int    has_at_stmt_list:1;
  171.   unsigned int    short_element_list:1;
  172. };
  173.  
  174. static int diecount;    /* Approximate count of dies for compilation unit */
  175. static struct dieinfo *curdie;    /* For warnings and such */
  176.  
  177. static char *dbbase;    /* Base pointer to dwarf info */
  178. static int dbroff;    /* Relative offset from start of .debug section */
  179. static char *lnbase;    /* Base pointer to line section */
  180. static int isreg;    /* Kludge to identify register variables */
  181. static int offreg;    /* Kludge to identify basereg references */
  182.  
  183. static CORE_ADDR baseaddr;    /* Add to each symbol value */
  184.  
  185. /* Each partial symbol table entry contains a pointer to private data for the
  186.    read_symtab() function to use when expanding a partial symbol table entry
  187.    to a full symbol table entry.  For DWARF debugging info, this data is
  188.    contained in the following structure and macros are provided for easy
  189.    access to the members given a pointer to a partial symbol table entry.
  190.  
  191.    dbfoff    Always the absolute file offset to the start of the ".debug"
  192.         section for the file containing the DIE's being accessed.
  193.  
  194.    dbroff    Relative offset from the start of the ".debug" access to the
  195.         first DIE to be accessed.  When building the partial symbol
  196.         table, this value will be zero since we are accessing the
  197.         entire ".debug" section.  When expanding a partial symbol
  198.         table entry, this value will be the offset to the first
  199.         DIE for the compilation unit containing the symbol that
  200.         triggers the expansion.
  201.  
  202.    dblength    The size of the chunk of DIE's being examined, in bytes.
  203.  
  204.    lnfoff    The absolute file offset to the line table fragment.  Ignored
  205.         when building partial symbol tables, but used when expanding
  206.         them, and contains the absolute file offset to the fragment
  207.         of the ".line" section containing the line numbers for the
  208.         current compilation unit.
  209.  */
  210.  
  211. struct dwfinfo {
  212.   int dbfoff;        /* Absolute file offset to start of .debug section */
  213.   int dbroff;        /* Relative offset from start of .debug section */
  214.   int dblength;        /* Size of the chunk of DIE's being examined */
  215.   int lnfoff;        /* Absolute file offset to line table fragment */
  216. };
  217.  
  218. #define DBFOFF(p) (((struct dwfinfo *)((p)->read_symtab_private))->dbfoff)
  219. #define DBROFF(p) (((struct dwfinfo *)((p)->read_symtab_private))->dbroff)
  220. #define DBLENGTH(p) (((struct dwfinfo *)((p)->read_symtab_private))->dblength)
  221. #define LNFOFF(p) (((struct dwfinfo *)((p)->read_symtab_private))->lnfoff)
  222.  
  223. /* The generic symbol table building routines have separate lists for
  224.    file scope symbols and all all other scopes (local scopes).  So
  225.    we need to select the right one to pass to add_symbol_to_list().
  226.    We do it by keeping a pointer to the correct list in list_in_scope.
  227.  
  228.    FIXME:  The original dwarf code just treated the file scope as the first
  229.    local scope, and all other local scopes as nested local scopes, and worked
  230.    fine.  Check to see if we really need to distinguish these in buildsym.c */
  231.  
  232. struct pending **list_in_scope = &file_symbols;
  233.  
  234. /* DIES which have user defined types or modified user defined types refer to
  235.    other DIES for the type information.  Thus we need to associate the offset
  236.    of a DIE for a user defined type with a pointer to the type information.
  237.  
  238.    Originally this was done using a simple but expensive algorithm, with an
  239.    array of unsorted structures, each containing an offset/type-pointer pair.
  240.    This array was scanned linearly each time a lookup was done.  The result
  241.    was that gdb was spending over half it's startup time munging through this
  242.    array of pointers looking for a structure that had the right offset member.
  243.  
  244.    The second attempt used the same array of structures, but the array was
  245.    sorted using qsort each time a new offset/type was recorded, and a binary
  246.    search was used to find the type pointer for a given DIE offset.  This was
  247.    even slower, due to the overhead of sorting the array each time a new
  248.    offset/type pair was entered.
  249.  
  250.    The third attempt uses a fixed size array of type pointers, indexed by a
  251.    value derived from the DIE offset.  Since the minimum DIE size is 4 bytes,
  252.    we can divide any DIE offset by 4 to obtain a unique index into this fixed
  253.    size array.  Since each element is a 4 byte pointer, it takes exactly as
  254.    much memory to hold this array as to hold the DWARF info for a given
  255.    compilation unit.  But it gets freed as soon as we are done with it. */
  256.  
  257. static struct type **utypes;    /* Pointer to array of user type pointers */
  258. static int numutypes;        /* Max number of user type pointers */
  259.  
  260. /* Forward declarations of static functions so we don't have to worry
  261.    about ordering within this file.  */
  262.  
  263. static void
  264. add_enum_psymbol PARAMS ((struct dieinfo *, struct objfile *));
  265.  
  266. static void
  267. read_file_scope PARAMS ((struct dieinfo *, char *, char *, struct objfile *));
  268.  
  269. static void
  270. read_func_scope PARAMS ((struct dieinfo *, char *, char *, struct objfile *));
  271.  
  272. static void
  273. read_lexical_block_scope PARAMS ((struct dieinfo *, char *, char *,
  274.                   struct objfile *));
  275.  
  276. static void
  277. dwarfwarn ();
  278.  
  279. static void
  280. scan_partial_symbols PARAMS ((char *, char *, struct objfile *));
  281.  
  282. static void
  283. scan_compilation_units PARAMS ((char *, char *, char *, unsigned int,
  284.                 unsigned int, struct objfile *));
  285.  
  286. static void
  287. add_partial_symbol PARAMS ((struct dieinfo *, struct objfile *));
  288.  
  289. static void
  290. init_psymbol_list PARAMS ((struct objfile *, int));
  291.  
  292. static void
  293. basicdieinfo PARAMS ((struct dieinfo *, char *));
  294.  
  295. static void
  296. completedieinfo PARAMS ((struct dieinfo *));
  297.  
  298. static void
  299. dwarf_psymtab_to_symtab PARAMS ((struct partial_symtab *));
  300.  
  301. static void
  302. psymtab_to_symtab_1 PARAMS ((struct partial_symtab *));
  303.  
  304. static struct symtab *
  305. read_ofile_symtab PARAMS ((struct partial_symtab *));
  306.  
  307. static void
  308. process_dies PARAMS ((char *, char *, struct objfile *));
  309.  
  310. static void
  311. read_structure_scope PARAMS ((struct dieinfo *, char *, char *,
  312.                   struct objfile *));
  313.  
  314. static struct type *
  315. decode_array_element_type PARAMS ((char *));
  316.  
  317. static struct type *
  318. decode_subscr_data PARAMS ((char *, char *));
  319.  
  320. static void
  321. dwarf_read_array_type PARAMS ((struct dieinfo *));
  322.  
  323. static void
  324. read_tag_pointer_type PARAMS ((struct dieinfo *dip));
  325.  
  326. static void
  327. read_subroutine_type PARAMS ((struct dieinfo *, char *, char *));
  328.  
  329. static void
  330. read_enumeration PARAMS ((struct dieinfo *, char *, char *, struct objfile *));
  331.  
  332. static struct type *
  333. struct_type PARAMS ((struct dieinfo *, char *, char *, struct objfile *));
  334.  
  335. static struct type *
  336. enum_type PARAMS ((struct dieinfo *, struct objfile *));
  337.  
  338. static void
  339. decode_line_numbers PARAMS ((char *));
  340.  
  341. static struct type *
  342. decode_die_type PARAMS ((struct dieinfo *));
  343.  
  344. static struct type *
  345. decode_mod_fund_type PARAMS ((char *));
  346.  
  347. static struct type *
  348. decode_mod_u_d_type PARAMS ((char *));
  349.  
  350. static struct type *
  351. decode_modified_type PARAMS ((unsigned char *, unsigned int, int));
  352.  
  353. static struct type *
  354. decode_fund_type PARAMS ((unsigned int));
  355.  
  356. static char *
  357. create_name PARAMS ((char *, struct obstack *));
  358.  
  359. static struct type *
  360. lookup_utype PARAMS ((DIEREF));
  361.  
  362. static struct type *
  363. alloc_utype PARAMS ((DIEREF, struct type *));
  364.  
  365. static struct symbol *
  366. new_symbol PARAMS ((struct dieinfo *, struct objfile *));
  367.  
  368. static int
  369. locval PARAMS ((char *));
  370.  
  371. static void
  372. record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,
  373.                    struct objfile *));
  374.  
  375. /*
  376.  
  377. GLOBAL FUNCTION
  378.  
  379.     dwarf_build_psymtabs -- build partial symtabs from DWARF debug info
  380.  
  381. SYNOPSIS
  382.  
  383.     void dwarf_build_psymtabs (int desc, char *filename, CORE_ADDR addr,
  384.          int mainline, unsigned int dbfoff, unsigned int dbsize,
  385.          unsigned int lnoffset, unsigned int lnsize,
  386.          struct objfile *objfile)
  387.  
  388. DESCRIPTION
  389.  
  390.     This function is called upon to build partial symtabs from files
  391.     containing DIE's (Dwarf Information Entries) and DWARF line numbers.
  392.  
  393.     It is passed a file descriptor for an open file containing the DIES
  394.     and line number information, the corresponding filename for that
  395.     file, a base address for relocating the symbols, a flag indicating
  396.     whether or not this debugging information is from a "main symbol
  397.     table" rather than a shared library or dynamically linked file,
  398.     and file offset/size pairs for the DIE information and line number
  399.     information.
  400.  
  401. RETURNS
  402.  
  403.     No return value.
  404.  
  405.  */
  406.  
  407. void
  408. dwarf_build_psymtabs (desc, filename, addr, mainline, dbfoff, dbsize,
  409.               lnoffset, lnsize, objfile)
  410.      int desc;
  411.      char *filename;
  412.      CORE_ADDR addr;
  413.      int mainline;
  414.      unsigned int dbfoff;
  415.      unsigned int dbsize;
  416.      unsigned int lnoffset;
  417.      unsigned int lnsize;
  418.      struct objfile *objfile;
  419. {
  420.   struct cleanup *back_to;
  421.   
  422.   dbbase = xmalloc (dbsize);
  423.   dbroff = 0;
  424.   if ((lseek (desc, dbfoff, 0) != dbfoff) ||
  425.       (read (desc, dbbase, dbsize) != dbsize))
  426.     {
  427.       free (dbbase);
  428.       error ("can't read DWARF data from '%s'", filename);
  429.     }
  430.   back_to = make_cleanup (free, dbbase);
  431.   
  432.   /* If we are reinitializing, or if we have never loaded syms yet, init.
  433.      Since we have no idea how many DIES we are looking at, we just guess
  434.      some arbitrary value. */
  435.   
  436.   if (mainline || objfile->global_psymbols.size == 0 || objfile->static_psymbols.size == 0)
  437.     {
  438.       init_psymbol_list (objfile, 1024);
  439.     }
  440.   
  441.   /* Save the relocation factor where everybody can see it.  */
  442.  
  443.   baseaddr = addr;
  444.  
  445.   /* Follow the compilation unit sibling chain, building a partial symbol
  446.      table entry for each one.  Save enough information about each compilation
  447.      unit to locate the full DWARF information later. */
  448.   
  449.   scan_compilation_units (filename, dbbase, dbbase + dbsize,
  450.               dbfoff, lnoffset, objfile);
  451.   
  452.   do_cleanups (back_to);
  453. }
  454.  
  455.  
  456. /*
  457.  
  458. LOCAL FUNCTION
  459.  
  460.     record_minimal_symbol -- add entry to gdb's minimal symbol table
  461.  
  462. SYNOPSIS
  463.  
  464.     static void record_minimal_symbol (char *name, CORE_ADDR address,
  465.                       enum minimal_symbol_type ms_type,
  466.                       struct objfile *objfile)
  467.  
  468. DESCRIPTION
  469.  
  470.     Given a pointer to the name of a symbol that should be added to the
  471.     minimal symbol table, and the address associated with that
  472.     symbol, records this information for later use in building the
  473.     minimal symbol table.
  474.  
  475.  */
  476.  
  477. static void
  478. record_minimal_symbol (name, address, ms_type, objfile)
  479.      char *name;
  480.      CORE_ADDR address;
  481.      enum minimal_symbol_type ms_type;
  482.      struct objfile *objfile;
  483. {
  484.   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
  485.   prim_record_minimal_symbol (name, address, ms_type);
  486. }
  487.  
  488. /*
  489.  
  490. LOCAL FUNCTION
  491.  
  492.     dwarfwarn -- issue a DWARF related warning
  493.  
  494. DESCRIPTION
  495.  
  496.     Issue warnings about DWARF related things that aren't serious enough
  497.     to warrant aborting with an error, but should not be ignored either.
  498.     This includes things like detectable corruption in DIE's, missing
  499.     DIE's, unimplemented features, etc.
  500.  
  501.     In general, running across tags or attributes that we don't recognize
  502.     is not considered to be a problem and we should not issue warnings
  503.     about such.
  504.  
  505. NOTES
  506.  
  507.     We mostly follow the example of the error() routine, but without
  508.     returning to command level.  It is arguable about whether warnings
  509.     should be issued at all, and if so, where they should go (stdout or
  510.     stderr).
  511.  
  512.     We assume that curdie is valid and contains at least the basic
  513.     information for the DIE where the problem was noticed.
  514. */
  515.  
  516. static void
  517. dwarfwarn (va_alist)
  518.      va_dcl
  519. {
  520.   va_list ap;
  521.   char *fmt;
  522.   
  523.   va_start (ap);
  524.   fmt = va_arg (ap, char *);
  525.   warning_setup ();
  526.   fprintf (stderr, "warning: DWARF ref 0x%x: ", curdie -> dieref);
  527.   if (curdie -> at_name)
  528.     {
  529.       fprintf (stderr, "'%s': ", curdie -> at_name);
  530.     }
  531.   vfprintf (stderr, fmt, ap);
  532.   fprintf (stderr, "\n");
  533.   fflush (stderr);
  534.   va_end (ap);
  535. }
  536.  
  537. /*
  538.  
  539. LOCAL FUNCTION
  540.  
  541.     read_lexical_block_scope -- process all dies in a lexical block
  542.  
  543. SYNOPSIS
  544.  
  545.     static void read_lexical_block_scope (struct dieinfo *dip,
  546.         char *thisdie, char *enddie)
  547.  
  548. DESCRIPTION
  549.  
  550.     Process all the DIES contained within a lexical block scope.
  551.     Start a new scope, process the dies, and then close the scope.
  552.  
  553.  */
  554.  
  555. static void
  556. read_lexical_block_scope (dip, thisdie, enddie, objfile)
  557.      struct dieinfo *dip;
  558.      char *thisdie;
  559.      char *enddie;
  560.      struct objfile *objfile;
  561. {
  562.   register struct context_stack *new;
  563.  
  564.   (void) push_context (0, dip -> at_low_pc);
  565.   process_dies (thisdie + dip -> dielength, enddie, objfile);
  566.   new = pop_context ();
  567.   if (local_symbols != NULL)
  568.     {
  569.       finish_block (0, &local_symbols, new -> old_blocks, new -> start_addr,
  570.             dip -> at_high_pc, objfile);
  571.     }
  572.   local_symbols = new -> locals;
  573. }
  574.  
  575. /*
  576.  
  577. LOCAL FUNCTION
  578.  
  579.     lookup_utype -- look up a user defined type from die reference
  580.  
  581. SYNOPSIS
  582.  
  583.     static type *lookup_utype (DIEREF dieref)
  584.  
  585. DESCRIPTION
  586.  
  587.     Given a DIE reference, lookup the user defined type associated with
  588.     that DIE, if it has been registered already.  If not registered, then
  589.     return NULL.  Alloc_utype() can be called to register an empty
  590.     type for this reference, which will be filled in later when the
  591.     actual referenced DIE is processed.
  592.  */
  593.  
  594. static struct type *
  595. lookup_utype (dieref)
  596.      DIEREF dieref;
  597. {
  598.   struct type *type = NULL;
  599.   int utypeidx;
  600.   
  601.   utypeidx = (dieref - dbroff) / 4;
  602.   if ((utypeidx < 0) || (utypeidx >= numutypes))
  603.     {
  604.       dwarfwarn ("reference to DIE (0x%x) outside compilation unit", dieref);
  605.     }
  606.   else
  607.     {
  608.       type = *(utypes + utypeidx);
  609.     }
  610.   return (type);
  611. }
  612.  
  613.  
  614. /*
  615.  
  616. LOCAL FUNCTION
  617.  
  618.     alloc_utype  -- add a user defined type for die reference
  619.  
  620. SYNOPSIS
  621.  
  622.     static type *alloc_utype (DIEREF dieref, struct type *utypep)
  623.  
  624. DESCRIPTION
  625.  
  626.     Given a die reference DIEREF, and a possible pointer to a user
  627.     defined type UTYPEP, register that this reference has a user
  628.     defined type and either use the specified type in UTYPEP or
  629.     make a new empty type that will be filled in later.
  630.  
  631.     We should only be called after calling lookup_utype() to verify that
  632.     there is not currently a type registered for DIEREF.
  633.  */
  634.  
  635. static struct type *
  636. alloc_utype (dieref, utypep)
  637.      DIEREF dieref;
  638.      struct type *utypep;
  639. {
  640.   struct type **typep;
  641.   int utypeidx;
  642.   
  643.   utypeidx = (dieref - dbroff) / 4;
  644.   typep = utypes + utypeidx;
  645.   if ((utypeidx < 0) || (utypeidx >= numutypes))
  646.     {
  647.       utypep = lookup_fundamental_type (current_objfile, FT_INTEGER);
  648.       dwarfwarn ("reference to DIE (0x%x) outside compilation unit", dieref);
  649.     }
  650.   else if (*typep != NULL)
  651.     {
  652.       utypep = *typep;
  653.       SQUAWK (("internal error: dup user type allocation"));
  654.     }
  655.   else
  656.     {
  657.       if (utypep == NULL)
  658.     {
  659.       utypep = (struct type *)
  660.         obstack_alloc (¤t_objfile -> type_obstack,
  661.                sizeof (struct type));
  662.       (void) memset (utypep, 0, sizeof (struct type));
  663.       TYPE_OBJFILE (utypep) = current_objfile;
  664.     }
  665.       *typep = utypep;
  666.     }
  667.   return (utypep);
  668. }
  669.  
  670. /*
  671.  
  672. LOCAL FUNCTION
  673.  
  674.     decode_die_type -- return a type for a specified die
  675.  
  676. SYNOPSIS
  677.  
  678.     static struct type *decode_die_type (struct dieinfo *dip)
  679.  
  680. DESCRIPTION
  681.  
  682.     Given a pointer to a die information structure DIP, decode the
  683.     type of the die and return a pointer to the decoded type.  All
  684.     dies without specific types default to type int.
  685.  */
  686.  
  687. static struct type *
  688. decode_die_type (dip)
  689.      struct dieinfo *dip;
  690. {
  691.   struct type *type = NULL;
  692.   
  693.   if (dip -> at_fund_type != 0)
  694.     {
  695.       type = decode_fund_type (dip -> at_fund_type);
  696.     }
  697.   else if (dip -> at_mod_fund_type != NULL)
  698.     {
  699.       type = decode_mod_fund_type (dip -> at_mod_fund_type);
  700.     }
  701.   else if (dip -> at_user_def_type)
  702.     {
  703.       if ((type = lookup_utype (dip -> at_user_def_type)) == NULL)
  704.     {
  705.       type = alloc_utype (dip -> at_user_def_type, NULL);
  706.     }
  707.     }
  708.   else if (dip -> at_mod_u_d_type)
  709.     {
  710.       type = decode_mod_u_d_type (dip -> at_mod_u_d_type);
  711.     }
  712.   else
  713.     {
  714.       type = lookup_fundamental_type (current_objfile, FT_INTEGER);
  715.     }
  716.   return (type);
  717. }
  718.  
  719. /*
  720.  
  721. LOCAL FUNCTION
  722.  
  723.     struct_type -- compute and return the type for a struct or union
  724.  
  725. SYNOPSIS
  726.  
  727.     static struct type *struct_type (struct dieinfo *dip, char *thisdie,
  728.         char *enddie, struct objfile *objfile)
  729.  
  730. DESCRIPTION
  731.  
  732.     Given pointer to a die information structure for a die which
  733.     defines a union or structure (and MUST define one or the other),
  734.     and pointers to the raw die data that define the range of dies which
  735.     define the members, compute and return the user defined type for the
  736.     structure or union.
  737.  */
  738.  
  739. static struct type *
  740. struct_type (dip, thisdie, enddie, objfile)
  741.      struct dieinfo *dip;
  742.      char *thisdie;
  743.      char *enddie;
  744.      struct objfile *objfile;
  745. {
  746.   struct type *type;
  747.   struct nextfield {
  748.     struct nextfield *next;
  749.     struct field field;
  750.   };
  751.   struct nextfield *list = NULL;
  752.   struct nextfield *new;
  753.   int nfields = 0;
  754.   int n;
  755.   char *tpart1;
  756.   struct dieinfo mbr;
  757.   char *nextdie;
  758.   
  759.   if ((type = lookup_utype (dip -> dieref)) == NULL)
  760.     {
  761.       /* No forward references created an empty type, so install one now */
  762.       type = alloc_utype (dip -> dieref, NULL);
  763.     }
  764.   INIT_CPLUS_SPECIFIC(type);
  765.   switch (dip -> dietag)
  766.     {
  767.       case TAG_structure_type:
  768.         TYPE_CODE (type) = TYPE_CODE_STRUCT;
  769.     tpart1 = "struct";
  770.     break;
  771.       case TAG_union_type:
  772.     TYPE_CODE (type) = TYPE_CODE_UNION;
  773.     tpart1 = "union";
  774.     break;
  775.       default:
  776.     /* Should never happen */
  777.     TYPE_CODE (type) = TYPE_CODE_UNDEF;
  778.     tpart1 = "???";
  779.     SQUAWK (("missing structure or union tag"));
  780.     break;
  781.     }
  782.   /* Some compilers try to be helpful by inventing "fake" names for
  783.      anonymous enums, structures, and unions, like "~0fake" or ".0fake".
  784.      Thanks, but no thanks... */
  785.   if (dip -> at_name != NULL
  786.       && *dip -> at_name != '~'
  787.       && *dip -> at_name != '.')
  788.     {
  789.       TYPE_NAME (type) = obconcat (¤t_objfile -> type_obstack,
  790.                    tpart1, " ", dip -> at_name);
  791.     }
  792.   if (dip -> at_byte_size != 0)
  793.     {
  794.       TYPE_LENGTH (type) = dip -> at_byte_size;
  795.     }
  796.   thisdie += dip -> dielength;
  797.   while (thisdie < enddie)
  798.     {
  799.       basicdieinfo (&mbr, thisdie);
  800.       completedieinfo (&mbr);
  801.       if (mbr.dielength <= sizeof (long))
  802.     {
  803.       break;
  804.     }
  805.       else if (mbr.at_sibling != 0)
  806.     {
  807.       nextdie = dbbase + mbr.at_sibling - dbroff;
  808.     }
  809.       else
  810.     {
  811.       nextdie = thisdie + mbr.dielength;
  812.     }
  813.       switch (mbr.dietag)
  814.     {
  815.     case TAG_member:
  816.       /* Get space to record the next field's data.  */
  817.       new = (struct nextfield *) alloca (sizeof (struct nextfield));
  818.       new -> next = list;
  819.       list = new;
  820.       /* Save the data.  */
  821.       list -> field.name = savestring (mbr.at_name, strlen (mbr.at_name));
  822.       list -> field.type = decode_die_type (&mbr);
  823.       list -> field.bitpos = 8 * locval (mbr.at_location);
  824.       list -> field.bitsize = 0;
  825.       nfields++;
  826.       break;
  827.     default:
  828.       process_dies (thisdie, nextdie, objfile);
  829.       break;
  830.     }
  831.       thisdie = nextdie;
  832.     }
  833.   /* Now create the vector of fields, and record how big it is.  We may
  834.      not even have any fields, if this DIE was generated due to a reference
  835.      to an anonymous structure or union.  In this case, TYPE_FLAG_STUB is
  836.      set, which clues gdb in to the fact that it needs to search elsewhere
  837.      for the full structure definition. */
  838.   if (nfields == 0)
  839.     {
  840.       TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
  841.     }
  842.   else
  843.     {
  844.       TYPE_NFIELDS (type) = nfields;
  845.       TYPE_FIELDS (type) = (struct field *)
  846.     obstack_alloc (¤t_objfile -> type_obstack,
  847.                sizeof (struct field) * nfields);
  848.       /* Copy the saved-up fields into the field vector.  */
  849.       for (n = nfields; list; list = list -> next)
  850.     {
  851.       TYPE_FIELD (type, --n) = list -> field;
  852.     }    
  853.     }
  854.   return (type);
  855. }
  856.  
  857. /*
  858.  
  859. LOCAL FUNCTION
  860.  
  861.     read_structure_scope -- process all dies within struct or union
  862.  
  863. SYNOPSIS
  864.  
  865.     static void read_structure_scope (struct dieinfo *dip,
  866.         char *thisdie, char *enddie, struct objfile *objfile)
  867.  
  868. DESCRIPTION
  869.  
  870.     Called when we find the DIE that starts a structure or union
  871.     scope (definition) to process all dies that define the members
  872.     of the structure or union.  DIP is a pointer to the die info
  873.     struct for the DIE that names the structure or union.
  874.  
  875. NOTES
  876.  
  877.     Note that we need to call struct_type regardless of whether or not
  878.     the DIE has an at_name attribute, since it might be an anonymous
  879.     structure or union.  This gets the type entered into our set of
  880.     user defined types.
  881.  
  882.     However, if the structure is incomplete (an opaque struct/union)
  883.     then suppress creating a symbol table entry for it since gdb only
  884.     wants to find the one with the complete definition.  Note that if
  885.     it is complete, we just call new_symbol, which does it's own
  886.     checking about whether the struct/union is anonymous or not (and
  887.     suppresses creating a symbol table entry itself).
  888.     
  889.  */
  890.  
  891. static void
  892. read_structure_scope (dip, thisdie, enddie, objfile)
  893.      struct dieinfo *dip;
  894.      char *thisdie;
  895.      char *enddie;
  896.      struct objfile *objfile;
  897. {
  898.   struct type *type;
  899.   struct symbol *sym;
  900.   
  901.   type = struct_type (dip, thisdie, enddie, objfile);
  902.   if (!(TYPE_FLAGS (type) & TYPE_FLAG_STUB))
  903.     {
  904.       if ((sym = new_symbol (dip, objfile)) != NULL)
  905.     {
  906.       SYMBOL_TYPE (sym) = type;
  907.     }
  908.     }
  909. }
  910.  
  911. /*
  912.  
  913. LOCAL FUNCTION
  914.  
  915.     decode_array_element_type -- decode type of the array elements
  916.  
  917. SYNOPSIS
  918.  
  919.     static struct type *decode_array_element_type (char *scan, char *end)
  920.  
  921. DESCRIPTION
  922.  
  923.     As the last step in decoding the array subscript information for an
  924.     array DIE, we need to decode the type of the array elements.  We are
  925.     passed a pointer to this last part of the subscript information and
  926.     must return the appropriate type.  If the type attribute is not
  927.     recognized, just warn about the problem and return type int.
  928.  */
  929.  
  930. static struct type *
  931. decode_array_element_type (scan)
  932.      char *scan;
  933. {
  934.   struct type *typep;
  935.   short attribute;
  936.   DIEREF dieref;
  937.   unsigned short fundtype;
  938.   
  939.   /* FIXME, does this confuse the host and target sizeof's?  --gnu */
  940.   (void) memcpy (&attribute, scan, sizeof (short));
  941.   scan += sizeof (short);
  942.   switch (attribute)
  943.     {
  944.     case AT_fund_type:
  945.       (void) memcpy (&fundtype, scan, sizeof (short));
  946.       typep = decode_fund_type (fundtype);
  947.       break;
  948.     case AT_mod_fund_type:
  949.       typep = decode_mod_fund_type (scan);
  950.       break;
  951.     case AT_user_def_type:
  952.       (void) memcpy (&dieref, scan, sizeof (DIEREF));
  953.       if ((typep = lookup_utype (dieref)) == NULL)
  954.     {
  955.       typep = alloc_utype (dieref, NULL);
  956.     }
  957.       break;
  958.     case AT_mod_u_d_type:
  959.       typep = decode_mod_u_d_type (scan);
  960.       break;
  961.     default:
  962.       SQUAWK (("bad array element type attribute 0x%x", attribute));
  963.       typep = lookup_fundamental_type (current_objfile, FT_INTEGER);
  964.       break;
  965.     }
  966.   return (typep);
  967. }
  968.  
  969. /*
  970.  
  971. LOCAL FUNCTION
  972.  
  973.     decode_subscr_data -- decode array subscript and element type data
  974.  
  975. SYNOPSIS
  976.  
  977.     static struct type *decode_subscr_data (char *scan, char *end)
  978.  
  979. DESCRIPTION
  980.  
  981.     The array subscripts and the data type of the elements of an
  982.     array are described by a list of data items, stored as a block
  983.     of contiguous bytes.  There is a data item describing each array
  984.     dimension, and a final data item describing the element type.
  985.     The data items are ordered the same as their appearance in the
  986.     source (I.E. leftmost dimension first, next to leftmost second,
  987.     etc).
  988.  
  989.     We are passed a pointer to the start of the block of bytes
  990.     containing the data items, and a pointer to the first byte past
  991.     the data.  This function decodes the data and returns a type.
  992.  
  993. BUGS
  994.     FIXME:  This code only implements the forms currently used
  995.     by the AT&T and GNU C compilers.
  996.  
  997.     The end pointer is supplied for error checking, maybe we should
  998.     use it for that...
  999.  */
  1000.  
  1001. static struct type *
  1002. decode_subscr_data (scan, end)
  1003.      char *scan;
  1004.      char *end;
  1005. {
  1006.   struct type *typep = NULL;
  1007.   struct type *nexttype;
  1008.   int format;
  1009.   short fundtype;
  1010.   long lowbound;
  1011.   long highbound;
  1012.   
  1013.   format = *scan++;
  1014.   switch (format)
  1015.     {
  1016.     case FMT_ET:
  1017.       typep = decode_array_element_type (scan);
  1018.       break;
  1019.     case FMT_FT_C_C:
  1020.       (void) memcpy (&fundtype, scan, sizeof (short));
  1021.       scan += sizeof (short);
  1022.       if (fundtype != FT_integer && fundtype != FT_signed_integer
  1023.       && fundtype != FT_unsigned_integer)
  1024.     {
  1025.       SQUAWK (("array subscripts must be integral types, not type 0x%x",
  1026.              fundtype));
  1027.     }
  1028.       else
  1029.     {
  1030.       (void) memcpy (&lowbound, scan, sizeof (long));
  1031.       scan += sizeof (long);
  1032.       (void) memcpy (&highbound, scan, sizeof (long));
  1033.       scan += sizeof (long);
  1034.       nexttype = decode_subscr_data (scan, end);
  1035.       if (nexttype != NULL)
  1036.         {
  1037.           typep = (struct type *)
  1038.         obstack_alloc (¤t_objfile -> type_obstack,
  1039.                    sizeof (struct type));
  1040.           (void) memset (typep, 0, sizeof (struct type));
  1041.           TYPE_OBJFILE (typep) = current_objfile;
  1042.           TYPE_CODE (typep) = TYPE_CODE_ARRAY;
  1043.           TYPE_LENGTH (typep) = TYPE_LENGTH (nexttype);
  1044.           TYPE_LENGTH (typep) *= lowbound + highbound + 1;
  1045.           TYPE_TARGET_TYPE (typep) = nexttype;
  1046.         }            
  1047.     }
  1048.       break;
  1049.     case FMT_FT_C_X:
  1050.     case FMT_FT_X_C:
  1051.     case FMT_FT_X_X:
  1052.     case FMT_UT_C_C:
  1053.     case FMT_UT_C_X:
  1054.     case FMT_UT_X_C:
  1055.     case FMT_UT_X_X:
  1056.       SQUAWK (("array subscript format 0x%x not handled yet", format));
  1057.       break;
  1058.     default:
  1059.       SQUAWK (("unknown array subscript format %x", format));
  1060.       break;
  1061.     }
  1062.   return (typep);
  1063. }
  1064.  
  1065. /*
  1066.  
  1067. LOCAL FUNCTION
  1068.  
  1069.     dwarf_read_array_type -- read TAG_array_type DIE
  1070.  
  1071. SYNOPSIS
  1072.  
  1073.     static void dwarf_read_array_type (struct dieinfo *dip)
  1074.  
  1075. DESCRIPTION
  1076.  
  1077.     Extract all information from a TAG_array_type DIE and add to
  1078.     the user defined type vector.
  1079.  */
  1080.  
  1081. static void
  1082. dwarf_read_array_type (dip)
  1083.      struct dieinfo *dip;
  1084. {
  1085.   struct type *type;
  1086.   struct type *utype;
  1087.   char *sub;
  1088.   char *subend;
  1089.   short temp;
  1090.   
  1091.   if (dip -> at_ordering != ORD_row_major)
  1092.     {
  1093.       /* FIXME:  Can gdb even handle column major arrays? */
  1094.       SQUAWK (("array not row major; not handled correctly"));
  1095.     }
  1096.   if ((sub = dip -> at_subscr_data) != NULL)
  1097.     {
  1098.       (void) memcpy (&temp, sub, sizeof (short));
  1099.       subend = sub + sizeof (short) + temp;
  1100.       sub += sizeof (short);
  1101.       type = decode_subscr_data (sub, subend);
  1102.       if (type == NULL)
  1103.     {
  1104.       if ((utype = lookup_utype (dip -> dieref)) == NULL)
  1105.         {
  1106.           utype = alloc_utype (dip -> dieref, NULL);
  1107.         }
  1108.       TYPE_CODE (utype) = TYPE_CODE_ARRAY;
  1109.       TYPE_TARGET_TYPE (utype) = 
  1110.               lookup_fundamental_type (current_objfile, FT_INTEGER);
  1111.       TYPE_LENGTH (utype) = 1 * TYPE_LENGTH (TYPE_TARGET_TYPE (utype));
  1112.     }
  1113.       else
  1114.     {
  1115.       if ((utype = lookup_utype (dip -> dieref)) == NULL)
  1116.         {
  1117.           (void) alloc_utype (dip -> dieref, type);
  1118.         }
  1119.       else
  1120.         {
  1121.           TYPE_CODE (utype) = TYPE_CODE_ARRAY;
  1122.           TYPE_LENGTH (utype) = TYPE_LENGTH (type);
  1123.           TYPE_TARGET_TYPE (utype) = TYPE_TARGET_TYPE (type);
  1124.         }
  1125.     }
  1126.     }
  1127. }
  1128.  
  1129. /*
  1130.  
  1131. LOCAL FUNCTION
  1132.  
  1133.     read_tag_pointer_type -- read TAG_pointer_type DIE
  1134.  
  1135. SYNOPSIS
  1136.  
  1137.     static void read_tag_pointer_type (struct dieinfo *dip)
  1138.  
  1139. DESCRIPTION
  1140.  
  1141.     Extract all information from a TAG_pointer_type DIE and add to
  1142.     the user defined type vector.
  1143.  */
  1144.  
  1145. static void
  1146. read_tag_pointer_type (dip)
  1147.      struct dieinfo *dip;
  1148. {
  1149.   struct type *type;
  1150.   struct type *utype;
  1151.   
  1152.   type = decode_die_type (dip);
  1153.   if ((utype = lookup_utype (dip -> dieref)) == NULL)
  1154.     {
  1155.       utype = lookup_pointer_type (type);
  1156.       (void) alloc_utype (dip -> dieref, utype);
  1157.     }
  1158.   else
  1159.     {
  1160.       TYPE_TARGET_TYPE (utype) = type;
  1161.       TYPE_POINTER_TYPE (type) = utype;
  1162.  
  1163.       /* We assume the machine has only one representation for pointers!  */
  1164.       /* FIXME:  This confuses host<->target data representations, and is a
  1165.      poor assumption besides. */
  1166.       
  1167.       TYPE_LENGTH (utype) = sizeof (char *);
  1168.       TYPE_CODE (utype) = TYPE_CODE_PTR;
  1169.     }
  1170. }
  1171.  
  1172. /*
  1173.  
  1174. LOCAL FUNCTION
  1175.  
  1176.     read_subroutine_type -- process TAG_subroutine_type dies
  1177.  
  1178. SYNOPSIS
  1179.  
  1180.     static void read_subroutine_type (struct dieinfo *dip, char thisdie,
  1181.         char *enddie)
  1182.  
  1183. DESCRIPTION
  1184.  
  1185.     Handle DIES due to C code like:
  1186.  
  1187.     struct foo {
  1188.         int (*funcp)(int a, long l);  (Generates TAG_subroutine_type DIE)
  1189.         int b;
  1190.     };
  1191.  
  1192. NOTES
  1193.  
  1194.     The parameter DIES are currently ignored.  See if gdb has a way to
  1195.     include this info in it's type system, and decode them if so.  Is
  1196.     this what the type structure's "arg_types" field is for?  (FIXME)
  1197.  */
  1198.  
  1199. static void
  1200. read_subroutine_type (dip, thisdie, enddie)
  1201.      struct dieinfo *dip;
  1202.      char *thisdie;
  1203.      char *enddie;
  1204. {
  1205.   struct type *type;        /* Type that this function returns */
  1206.   struct type *ftype;        /* Function that returns above type */
  1207.   
  1208.   /* Decode the type that this subroutine returns */
  1209.  
  1210.   type = decode_die_type (dip);
  1211.  
  1212.   /* Check to see if we already have a partially constructed user
  1213.      defined type for this DIE, from a forward reference. */
  1214.  
  1215.   if ((ftype = lookup_utype (dip -> dieref)) == NULL)
  1216.     {
  1217.       /* This is the first reference to one of these types.  Make
  1218.      a new one and place it in the user defined types. */
  1219.       ftype = lookup_function_type (type);
  1220.       (void) alloc_utype (dip -> dieref, ftype);
  1221.     }
  1222.   else
  1223.     {
  1224.       /* We have an existing partially constructed type, so bash it
  1225.      into the correct type. */
  1226.       TYPE_TARGET_TYPE (ftype) = type;
  1227.       TYPE_FUNCTION_TYPE (type) = ftype;
  1228.       TYPE_LENGTH (ftype) = 1;
  1229.       TYPE_CODE (ftype) = TYPE_CODE_FUNC;
  1230.     }
  1231. }
  1232.  
  1233. /*
  1234.  
  1235. LOCAL FUNCTION
  1236.  
  1237.     read_enumeration -- process dies which define an enumeration
  1238.  
  1239. SYNOPSIS
  1240.  
  1241.     static void read_enumeration (struct dieinfo *dip, char *thisdie,
  1242.         char *enddie, struct objfile *objfile)
  1243.  
  1244. DESCRIPTION
  1245.  
  1246.     Given a pointer to a die which begins an enumeration, process all
  1247.     the dies that define the members of the enumeration.
  1248.  
  1249. NOTES
  1250.  
  1251.     Note that we need to call enum_type regardless of whether or not we
  1252.     have a symbol, since we might have an enum without a tag name (thus
  1253.     no symbol for the tagname).
  1254.  */
  1255.  
  1256. static void
  1257. read_enumeration (dip, thisdie, enddie, objfile)
  1258.      struct dieinfo *dip;
  1259.      char *thisdie;
  1260.      char *enddie;
  1261.      struct objfile *objfile;
  1262. {
  1263.   struct type *type;
  1264.   struct symbol *sym;
  1265.   
  1266.   type = enum_type (dip, objfile);
  1267.   if ((sym = new_symbol (dip, objfile)) != NULL)
  1268.     {
  1269.       SYMBOL_TYPE (sym) = type;
  1270.     }
  1271. }
  1272.  
  1273. /*
  1274.  
  1275. LOCAL FUNCTION
  1276.  
  1277.     enum_type -- decode and return a type for an enumeration
  1278.  
  1279. SYNOPSIS
  1280.  
  1281.     static type *enum_type (struct dieinfo *dip, struct objfile *objfile)
  1282.  
  1283. DESCRIPTION
  1284.  
  1285.     Given a pointer to a die information structure for the die which
  1286.     starts an enumeration, process all the dies that define the members
  1287.     of the enumeration and return a type pointer for the enumeration.
  1288.  
  1289.     At the same time, for each member of the enumeration, create a
  1290.     symbol for it with namespace VAR_NAMESPACE and class LOC_CONST,
  1291.     and give it the type of the enumeration itself.
  1292.  
  1293. NOTES
  1294.  
  1295.     Note that the DWARF specification explicitly mandates that enum
  1296.     constants occur in reverse order from the source program order,
  1297.     for "consistency" and because this ordering is easier for many
  1298.     compilers to generate. (Draft 6, sec 3.8.5, Enumeration type
  1299.     Entries).  Because gdb wants to see the enum members in program
  1300.     source order, we have to ensure that the order gets reversed while
  1301.     we are processing them.
  1302.  */
  1303.  
  1304. static struct type *
  1305. enum_type (dip, objfile)
  1306.      struct dieinfo *dip;
  1307.      struct objfile *objfile;
  1308. {
  1309.   struct type *type;
  1310.   struct nextfield {
  1311.     struct nextfield *next;
  1312.     struct field field;
  1313.   };
  1314.   struct nextfield *list = NULL;
  1315.   struct nextfield *new;
  1316.   int nfields = 0;
  1317.   int n;
  1318.   char *scan;
  1319.   char *listend;
  1320.   long ltemp;
  1321.   short stemp;
  1322.   struct symbol *sym;
  1323.   
  1324.   if ((type = lookup_utype (dip -> dieref)) == NULL)
  1325.     {
  1326.       /* No forward references created an empty type, so install one now */
  1327.       type = alloc_utype (dip -> dieref, NULL);
  1328.     }
  1329.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  1330.   /* Some compilers try to be helpful by inventing "fake" names for
  1331.      anonymous enums, structures, and unions, like "~0fake" or ".0fake".
  1332.      Thanks, but no thanks... */
  1333.   if (dip -> at_name != NULL
  1334.       && *dip -> at_name != '~'
  1335.       && *dip -> at_name != '.')
  1336.     {
  1337.       TYPE_NAME (type) = obconcat (¤t_objfile -> type_obstack, "enum",
  1338.                    " ", dip -> at_name);
  1339.     }
  1340.   if (dip -> at_byte_size != 0)
  1341.     {
  1342.       TYPE_LENGTH (type) = dip -> at_byte_size;
  1343.     }
  1344.   if ((scan = dip -> at_element_list) != NULL)
  1345.     {
  1346.       if (dip -> short_element_list)
  1347.     {
  1348.       (void) memcpy (&stemp, scan, sizeof (stemp));
  1349.       listend = scan + stemp + sizeof (stemp);
  1350.       scan += sizeof (stemp);
  1351.     }
  1352.       else
  1353.     {
  1354.       (void) memcpy (<emp, scan, sizeof (ltemp));
  1355.       listend = scan + ltemp + sizeof (ltemp);
  1356.       scan += sizeof (ltemp);
  1357.     }
  1358.       while (scan < listend)
  1359.     {
  1360.       new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1361.       new -> next = list;
  1362.       list = new;
  1363.       list -> field.type = NULL;
  1364.       list -> field.bitsize = 0;
  1365.       (void) memcpy (&list -> field.bitpos, scan, sizeof (long));
  1366.       scan += sizeof (long);
  1367.       list -> field.name = savestring (scan, strlen (scan));
  1368.       scan += strlen (scan) + 1;
  1369.       nfields++;
  1370.       /* Handcraft a new symbol for this enum member. */
  1371.       sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
  1372.                          sizeof (struct symbol));
  1373.       (void) memset (sym, 0, sizeof (struct symbol));
  1374.       SYMBOL_NAME (sym) = create_name (list -> field.name, &objfile->symbol_obstack);
  1375.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1376.       SYMBOL_CLASS (sym) = LOC_CONST;
  1377.       SYMBOL_TYPE (sym) = type;
  1378.       SYMBOL_VALUE (sym) = list -> field.bitpos;
  1379.       add_symbol_to_list (sym, list_in_scope);
  1380.     }
  1381.       /* Now create the vector of fields, and record how big it is. This is
  1382.      where we reverse the order, by pulling the members of the list in
  1383.      reverse order from how they were inserted.  If we have no fields
  1384.      (this is apparently possible in C++) then skip building a field
  1385.      vector. */
  1386.       if (nfields > 0)
  1387.     {
  1388.       TYPE_NFIELDS (type) = nfields;
  1389.       TYPE_FIELDS (type) = (struct field *)
  1390.         obstack_alloc (&objfile->symbol_obstack, sizeof (struct field) * nfields);
  1391.       /* Copy the saved-up fields into the field vector.  */
  1392.       for (n = 0; (n < nfields) && (list != NULL); list = list -> next)
  1393.         {
  1394.           TYPE_FIELD (type, n++) = list -> field;
  1395.         }    
  1396.     }
  1397.     }
  1398.   return (type);
  1399. }
  1400.  
  1401. /*
  1402.  
  1403. LOCAL FUNCTION
  1404.  
  1405.     read_func_scope -- process all dies within a function scope
  1406.  
  1407. DESCRIPTION
  1408.  
  1409.     Process all dies within a given function scope.  We are passed
  1410.     a die information structure pointer DIP for the die which
  1411.     starts the function scope, and pointers into the raw die data
  1412.     that define the dies within the function scope.
  1413.  
  1414.     For now, we ignore lexical block scopes within the function.
  1415.     The problem is that AT&T cc does not define a DWARF lexical
  1416.     block scope for the function itself, while gcc defines a
  1417.     lexical block scope for the function.  We need to think about
  1418.     how to handle this difference, or if it is even a problem.
  1419.     (FIXME)
  1420.  */
  1421.  
  1422. static void
  1423. read_func_scope (dip, thisdie, enddie, objfile)
  1424.      struct dieinfo *dip;
  1425.      char *thisdie;
  1426.      char *enddie;
  1427.      struct objfile *objfile;
  1428. {
  1429.   register struct context_stack *new;
  1430.   
  1431.   if (objfile -> ei.entry_point >= dip -> at_low_pc &&
  1432.       objfile -> ei.entry_point <  dip -> at_high_pc)
  1433.     {
  1434.       objfile -> ei.entry_func_lowpc = dip -> at_low_pc;
  1435.       objfile -> ei.entry_func_highpc = dip -> at_high_pc;
  1436.     }
  1437.   if (STREQ (dip -> at_name, "main"))    /* FIXME: hardwired name */
  1438.     {
  1439.       objfile -> ei.main_func_lowpc = dip -> at_low_pc;
  1440.       objfile -> ei.main_func_highpc = dip -> at_high_pc;
  1441.     }
  1442.   new = push_context (0, dip -> at_low_pc);
  1443.   new -> name = new_symbol (dip, objfile);
  1444.   list_in_scope = &local_symbols;
  1445.   process_dies (thisdie + dip -> dielength, enddie, objfile);
  1446.   new = pop_context ();
  1447.   /* Make a block for the local symbols within.  */
  1448.   finish_block (new -> name, &local_symbols, new -> old_blocks,
  1449.         new -> start_addr, dip -> at_high_pc, objfile);
  1450.   list_in_scope = &file_symbols;
  1451. }
  1452.  
  1453. /*
  1454.  
  1455. LOCAL FUNCTION
  1456.  
  1457.     read_file_scope -- process all dies within a file scope
  1458.  
  1459. DESCRIPTION
  1460.  
  1461.     Process all dies within a given file scope.  We are passed a
  1462.     pointer to the die information structure for the die which
  1463.     starts the file scope, and pointers into the raw die data which
  1464.     mark the range of dies within the file scope.
  1465.  
  1466.     When the partial symbol table is built, the file offset for the line
  1467.     number table for each compilation unit is saved in the partial symbol
  1468.     table entry for that compilation unit.  As the symbols for each
  1469.     compilation unit are read, the line number table is read into memory
  1470.     and the variable lnbase is set to point to it.  Thus all we have to
  1471.     do is use lnbase to access the line number table for the current
  1472.     compilation unit.
  1473.  */
  1474.  
  1475. static void
  1476. read_file_scope (dip, thisdie, enddie, objfile)
  1477.      struct dieinfo *dip;
  1478.      char *thisdie;
  1479.      char *enddie;
  1480.      struct objfile *objfile;
  1481. {
  1482.   struct cleanup *back_to;
  1483.   struct symtab *symtab;
  1484.   
  1485.   if (objfile -> ei.entry_point >= dip -> at_low_pc &&
  1486.       objfile -> ei.entry_point <  dip -> at_high_pc)
  1487.     {
  1488.       objfile -> ei.entry_file_lowpc = dip -> at_low_pc;
  1489.       objfile -> ei.entry_file_highpc = dip -> at_high_pc;
  1490.     }
  1491.   if (dip -> at_producer != NULL)
  1492.     {
  1493.       processing_gcc_compilation =
  1494.     STREQN (dip -> at_producer, GCC_PRODUCER, strlen (GCC_PRODUCER));
  1495.     }
  1496.   numutypes = (enddie - thisdie) / 4;
  1497.   utypes = (struct type **) xmalloc (numutypes * sizeof (struct type *));
  1498.   back_to = make_cleanup (free, utypes);
  1499.   (void) memset (utypes, 0, numutypes * sizeof (struct type *));
  1500.   start_symtab (dip -> at_name, NULL, dip -> at_low_pc);
  1501.   decode_line_numbers (lnbase);
  1502.   process_dies (thisdie + dip -> dielength, enddie, objfile);
  1503.   symtab = end_symtab (dip -> at_high_pc, 0, 0, objfile);
  1504.   /* FIXME:  The following may need to be expanded for other languages */
  1505.   switch (dip -> at_language)
  1506.     {
  1507.       case LANG_C89:
  1508.       case LANG_C:
  1509.     symtab -> language = language_c;
  1510.     break;
  1511.       case LANG_C_PLUS_PLUS:
  1512.     symtab -> language = language_cplus;
  1513.     break;
  1514.       default:
  1515.     ;
  1516.     }
  1517.   do_cleanups (back_to);
  1518.   utypes = NULL;
  1519.   numutypes = 0;
  1520. }
  1521.  
  1522. /*
  1523.  
  1524. LOCAL FUNCTION
  1525.  
  1526.     process_dies -- process a range of DWARF Information Entries
  1527.  
  1528. SYNOPSIS
  1529.  
  1530.     static void process_dies (char *thisdie, char *enddie,
  1531.                   struct objfile *objfile)
  1532.  
  1533. DESCRIPTION
  1534.  
  1535.     Process all DIE's in a specified range.  May be (and almost
  1536.     certainly will be) called recursively.
  1537.  */
  1538.  
  1539. static void
  1540. process_dies (thisdie, enddie, objfile)
  1541.      char *thisdie;
  1542.      char *enddie;
  1543.      struct objfile *objfile;
  1544. {
  1545.   char *nextdie;
  1546.   struct dieinfo di;
  1547.   
  1548.   while (thisdie < enddie)
  1549.     {
  1550.       basicdieinfo (&di, thisdie);
  1551.       if (di.dielength < sizeof (long))
  1552.     {
  1553.       break;
  1554.     }
  1555.       else if (di.dietag == TAG_padding)
  1556.     {
  1557.       nextdie = thisdie + di.dielength;
  1558.     }
  1559.       else
  1560.     {
  1561.       completedieinfo (&di);
  1562.       if (di.at_sibling != 0)
  1563.         {
  1564.           nextdie = dbbase + di.at_sibling - dbroff;
  1565.         }
  1566.       else
  1567.         {
  1568.           nextdie = thisdie + di.dielength;
  1569.         }
  1570.       switch (di.dietag)
  1571.         {
  1572.         case TAG_compile_unit:
  1573.           read_file_scope (&di, thisdie, nextdie, objfile);
  1574.           break;
  1575.         case TAG_global_subroutine:
  1576.         case TAG_subroutine:
  1577.           if (di.has_at_low_pc)
  1578.         {
  1579.           read_func_scope (&di, thisdie, nextdie, objfile);
  1580.         }
  1581.           break;
  1582.         case TAG_lexical_block:
  1583.           read_lexical_block_scope (&di, thisdie, nextdie, objfile);
  1584.           break;
  1585.         case TAG_structure_type:
  1586.         case TAG_union_type:
  1587.           read_structure_scope (&di, thisdie, nextdie, objfile);
  1588.           break;
  1589.         case TAG_enumeration_type:
  1590.           read_enumeration (&di, thisdie, nextdie, objfile);
  1591.           break;
  1592.         case TAG_subroutine_type:
  1593.           read_subroutine_type (&di, thisdie, nextdie);
  1594.           break;
  1595.         case TAG_array_type:
  1596.           dwarf_read_array_type (&di);
  1597.           break;
  1598.         case TAG_pointer_type:
  1599.           read_tag_pointer_type (&di);
  1600.           break;
  1601.         default:
  1602.           (void) new_symbol (&di, objfile);
  1603.           break;
  1604.         }
  1605.     }
  1606.       thisdie = nextdie;
  1607.     }
  1608. }
  1609.  
  1610. /*
  1611.  
  1612. LOCAL FUNCTION
  1613.  
  1614.     decode_line_numbers -- decode a line number table fragment
  1615.  
  1616. SYNOPSIS
  1617.  
  1618.     static void decode_line_numbers (char *tblscan, char *tblend,
  1619.         long length, long base, long line, long pc)
  1620.  
  1621. DESCRIPTION
  1622.  
  1623.     Translate the DWARF line number information to gdb form.
  1624.  
  1625.     The ".line" section contains one or more line number tables, one for
  1626.     each ".line" section from the objects that were linked.
  1627.  
  1628.     The AT_stmt_list attribute for each TAG_source_file entry in the
  1629.     ".debug" section contains the offset into the ".line" section for the
  1630.     start of the table for that file.
  1631.  
  1632.     The table itself has the following structure:
  1633.  
  1634.     <table length><base address><source statement entry>
  1635.     4 bytes       4 bytes       10 bytes
  1636.  
  1637.     The table length is the total size of the table, including the 4 bytes
  1638.     for the length information.
  1639.  
  1640.     The base address is the address of the first instruction generated
  1641.     for the source file.
  1642.  
  1643.     Each source statement entry has the following structure:
  1644.  
  1645.     <line number><statement position><address delta>
  1646.     4 bytes      2 bytes             4 bytes
  1647.  
  1648.     The line number is relative to the start of the file, starting with
  1649.     line 1.
  1650.  
  1651.     The statement position either -1 (0xFFFF) or the number of characters
  1652.     from the beginning of the line to the beginning of the statement.
  1653.  
  1654.     The address delta is the difference between the base address and
  1655.     the address of the first instruction for the statement.
  1656.  
  1657.     Note that we must copy the bytes from the packed table to our local
  1658.     variables before attempting to use them, to avoid alignment problems
  1659.     on some machines, particularly RISC processors.
  1660.  
  1661. BUGS
  1662.  
  1663.     Does gdb expect the line numbers to be sorted?  They are now by
  1664.     chance/luck, but are not required to be.  (FIXME)
  1665.  
  1666.     The line with number 0 is unused, gdb apparently can discover the
  1667.     span of the last line some other way. How?  (FIXME)
  1668.  */
  1669.  
  1670. static void
  1671. decode_line_numbers (linetable)
  1672.      char *linetable;
  1673. {
  1674.   char *tblscan;
  1675.   char *tblend;
  1676.   long length;
  1677.   long base;
  1678.   long line;
  1679.   long pc;
  1680.   
  1681.   if (linetable != NULL)
  1682.     {
  1683.       tblscan = tblend = linetable;
  1684.       (void) memcpy (&length, tblscan, sizeof (long));
  1685.       tblscan += sizeof (long);
  1686.       tblend += length;
  1687.       (void) memcpy (&base, tblscan, sizeof (long));
  1688.       base += baseaddr;
  1689.       tblscan += sizeof (long);
  1690.       while (tblscan < tblend)
  1691.     {
  1692.       (void) memcpy (&line, tblscan, sizeof (long));
  1693.       tblscan += sizeof (long) + sizeof (short);
  1694.       (void) memcpy (&pc, tblscan, sizeof (long));
  1695.       tblscan += sizeof (long);
  1696.       pc += base;
  1697.       if (line > 0)
  1698.         {
  1699.           record_line (current_subfile, line, pc);
  1700.         }
  1701.     }
  1702.     }
  1703. }
  1704.  
  1705. /*
  1706.  
  1707. LOCAL FUNCTION
  1708.  
  1709.     locval -- compute the value of a location attribute
  1710.  
  1711. SYNOPSIS
  1712.  
  1713.     static int locval (char *loc)
  1714.  
  1715. DESCRIPTION
  1716.  
  1717.     Given pointer to a string of bytes that define a location, compute
  1718.     the location and return the value.
  1719.  
  1720.     When computing values involving the current value of the frame pointer,
  1721.     the value zero is used, which results in a value relative to the frame
  1722.     pointer, rather than the absolute value.  This is what GDB wants
  1723.     anyway.
  1724.     
  1725.     When the result is a register number, the global isreg flag is set,
  1726.     otherwise it is cleared.  This is a kludge until we figure out a better
  1727.     way to handle the problem.  Gdb's design does not mesh well with the
  1728.     DWARF notion of a location computing interpreter, which is a shame
  1729.     because the flexibility goes unused.
  1730.  
  1731. NOTES
  1732.  
  1733.     Note that stack[0] is unused except as a default error return.
  1734.     Note that stack overflow is not yet handled.
  1735.  */
  1736.  
  1737. static int
  1738. locval (loc)
  1739.      char *loc;
  1740. {
  1741.   unsigned short nbytes;
  1742.   auto int stack[64];
  1743.   int stacki;
  1744.   char *end;
  1745.   long regno;
  1746.   
  1747.   (void) memcpy (&nbytes, loc, sizeof (short));
  1748.   end = loc + sizeof (short) + nbytes;
  1749.   stacki = 0;
  1750.   stack[stacki] = 0;
  1751.   isreg = 0;
  1752.   offreg = 0;
  1753.   for (loc += sizeof (short); loc < end; loc += sizeof (long))
  1754.     {
  1755.       switch (*loc++) {
  1756.       case 0:
  1757.     /* error */
  1758.     loc = end;
  1759.     break;
  1760.       case OP_REG:
  1761.     /* push register (number) */
  1762.     (void) memcpy (&stack[++stacki], loc, sizeof (long));
  1763.     isreg = 1;
  1764.     break;
  1765.       case OP_BASEREG:
  1766.     /* push value of register (number) */
  1767.     /* Actually, we compute the value as if register has 0 */
  1768.     offreg = 1;
  1769.     (void) memcpy (®no, loc, sizeof (long));
  1770.     if (regno == R_FP)
  1771.       {
  1772.         stack[++stacki] = 0;
  1773.       }
  1774.     else
  1775.       {
  1776.         stack[++stacki] = 0;
  1777.         SQUAWK (("BASEREG %d not handled!", regno));
  1778.       }
  1779.     break;
  1780.       case OP_ADDR:
  1781.     /* push address (relocated address) */
  1782.     (void) memcpy (&stack[++stacki], loc, sizeof (long));
  1783.     break;
  1784.       case OP_CONST:
  1785.     /* push constant (number) */
  1786.     (void) memcpy (&stack[++stacki], loc, sizeof (long));
  1787.     break;
  1788.       case OP_DEREF2:
  1789.     /* pop, deref and push 2 bytes (as a long) */
  1790.     SQUAWK (("OP_DEREF2 address %#x not handled", stack[stacki]));
  1791.     break;
  1792.       case OP_DEREF4:    /* pop, deref and push 4 bytes (as a long) */
  1793.     SQUAWK (("OP_DEREF4 address %#x not handled", stack[stacki]));
  1794.     break;
  1795.       case OP_ADD:    /* pop top 2 items, add, push result */
  1796.     stack[stacki - 1] += stack[stacki];
  1797.     stacki--;
  1798.     break;
  1799.       }
  1800.     }
  1801.   return (stack[stacki]);
  1802. }
  1803.  
  1804. /*
  1805.  
  1806. LOCAL FUNCTION
  1807.  
  1808.     read_ofile_symtab -- build a full symtab entry from chunk of DIE's
  1809.  
  1810. SYNOPSIS
  1811.  
  1812.     static struct symtab *read_ofile_symtab (struct partial_symtab *pst)
  1813.  
  1814. DESCRIPTION
  1815.  
  1816.     When expanding a partial symbol table entry to a full symbol table
  1817.     entry, this is the function that gets called to read in the symbols
  1818.     for the compilation unit.
  1819.  
  1820.     Returns a pointer to the newly constructed symtab (which is now
  1821.     the new first one on the objfile's symtab list).
  1822.  */
  1823.  
  1824. static struct symtab *
  1825. read_ofile_symtab (pst)
  1826.      struct partial_symtab *pst;
  1827. {
  1828.   struct cleanup *back_to;
  1829.   long lnsize;
  1830.   int foffset;
  1831.   bfd *abfd;
  1832.  
  1833.   abfd = pst -> objfile -> obfd;
  1834.   current_objfile = pst -> objfile;
  1835.  
  1836.   /* Allocate a buffer for the entire chunk of DIE's for this compilation
  1837.      unit, seek to the location in the file, and read in all the DIE's. */
  1838.  
  1839.   diecount = 0;
  1840.   dbbase = xmalloc (DBLENGTH(pst));
  1841.   dbroff = DBROFF(pst);
  1842.   foffset = DBFOFF(pst) + dbroff;
  1843.   baseaddr = pst -> addr;
  1844.   if (bfd_seek (abfd, foffset, 0) ||
  1845.       (bfd_read (dbbase, DBLENGTH(pst), 1, abfd) != DBLENGTH(pst)))
  1846.     {
  1847.       free (dbbase);
  1848.       error ("can't read DWARF data");
  1849.     }
  1850.   back_to = make_cleanup (free, dbbase);
  1851.  
  1852.   /* If there is a line number table associated with this compilation unit
  1853.      then read the first long word from the line number table fragment, which
  1854.      contains the size of the fragment in bytes (including the long word
  1855.      itself).  Allocate a buffer for the fragment and read it in for future
  1856.      processing. */
  1857.  
  1858.   lnbase = NULL;
  1859.   if (LNFOFF (pst))
  1860.     {
  1861.       if (bfd_seek (abfd, LNFOFF (pst), 0) ||
  1862.       (bfd_read ((PTR)&lnsize, sizeof(long), 1, abfd) != sizeof(long)))
  1863.     {
  1864.       error ("can't read DWARF line number table size");
  1865.     }
  1866.       lnbase = xmalloc (lnsize);
  1867.       if (bfd_seek (abfd, LNFOFF (pst), 0) ||
  1868.       (bfd_read (lnbase, lnsize, 1, abfd) != lnsize))
  1869.     {
  1870.       free (lnbase);
  1871.       error ("can't read DWARF line numbers");
  1872.     }
  1873.       make_cleanup (free, lnbase);
  1874.     }
  1875.  
  1876.   process_dies (dbbase, dbbase + DBLENGTH(pst), pst -> objfile);
  1877.   do_cleanups (back_to);
  1878.   current_objfile = NULL;
  1879.   return (pst -> objfile -> symtabs);
  1880. }
  1881.  
  1882. /*
  1883.  
  1884. LOCAL FUNCTION
  1885.  
  1886.     psymtab_to_symtab_1 -- do grunt work for building a full symtab entry
  1887.  
  1888. SYNOPSIS
  1889.  
  1890.     static void psymtab_to_symtab_1 (struct partial_symtab *pst)
  1891.  
  1892. DESCRIPTION
  1893.  
  1894.     Called once for each partial symbol table entry that needs to be
  1895.     expanded into a full symbol table entry.
  1896.  
  1897. */
  1898.  
  1899. static void
  1900. psymtab_to_symtab_1 (pst)
  1901.      struct partial_symtab *pst;
  1902. {
  1903.   int i;
  1904.   
  1905.   if (pst != NULL)
  1906.     {
  1907.       if (pst->readin)
  1908.     {
  1909.       warning ("psymtab for %s already read in.  Shouldn't happen.",
  1910.            pst -> filename);
  1911.     }
  1912.       else
  1913.     {
  1914.       /* Read in all partial symtabs on which this one is dependent */
  1915.       for (i = 0; i < pst -> number_of_dependencies; i++)
  1916.         {
  1917.           if (!pst -> dependencies[i] -> readin)
  1918.         {
  1919.           /* Inform about additional files that need to be read in. */
  1920.           if (info_verbose)
  1921.             {
  1922.               fputs_filtered (" ", stdout);
  1923.               wrap_here ("");
  1924.               fputs_filtered ("and ", stdout);
  1925.               wrap_here ("");
  1926.               printf_filtered ("%s...",
  1927.                        pst -> dependencies[i] -> filename);
  1928.               wrap_here ("");
  1929.               fflush (stdout);        /* Flush output */
  1930.             }
  1931.           psymtab_to_symtab_1 (pst -> dependencies[i]);
  1932.         }
  1933.         }      
  1934.       if (DBLENGTH (pst))        /* Otherwise it's a dummy */
  1935.         {
  1936.           pst -> symtab = read_ofile_symtab (pst);
  1937.           if (info_verbose)
  1938.         {
  1939.           printf_filtered ("%d DIE's, sorting...", diecount);
  1940.           wrap_here ("");
  1941.           fflush (stdout);
  1942.         }
  1943.           sort_symtab_syms (pst -> symtab);
  1944.         }
  1945.       pst -> readin = 1;
  1946.     }
  1947.     }
  1948. }
  1949.  
  1950. /*
  1951.  
  1952. LOCAL FUNCTION
  1953.  
  1954.     dwarf_psymtab_to_symtab -- build a full symtab entry from partial one
  1955.  
  1956. SYNOPSIS
  1957.  
  1958.     static void dwarf_psymtab_to_symtab (struct partial_symtab *pst)
  1959.  
  1960. DESCRIPTION
  1961.  
  1962.     This is the DWARF support entry point for building a full symbol
  1963.     table entry from a partial symbol table entry.  We are passed a
  1964.     pointer to the partial symbol table entry that needs to be expanded.
  1965.  
  1966. */
  1967.  
  1968. static void
  1969. dwarf_psymtab_to_symtab (pst)
  1970.      struct partial_symtab *pst;
  1971. {
  1972.  
  1973.   if (pst != NULL)
  1974.     {
  1975.       if (pst -> readin)
  1976.     {
  1977.       warning ("psymtab for %s already read in.  Shouldn't happen.",
  1978.            pst -> filename);
  1979.     }
  1980.       else
  1981.     {
  1982.       if (DBLENGTH (pst) || pst -> number_of_dependencies)
  1983.         {
  1984.           /* Print the message now, before starting serious work, to avoid
  1985.          disconcerting pauses.  */
  1986.           if (info_verbose)
  1987.         {
  1988.           printf_filtered ("Reading in symbols for %s...",
  1989.                    pst -> filename);
  1990.           fflush (stdout);
  1991.         }
  1992.           
  1993.           psymtab_to_symtab_1 (pst);
  1994.           
  1995. #if 0          /* FIXME:  Check to see what dbxread is doing here and see if
  1996.          we need to do an equivalent or is this something peculiar to
  1997.          stabs/a.out format.
  1998.          Match with global symbols.  This only needs to be done once,
  1999.          after all of the symtabs and dependencies have been read in.
  2000.          */
  2001.           scan_file_globals (pst -> objfile);
  2002. #endif
  2003.           
  2004.           /* Finish up the verbose info message.  */
  2005.           if (info_verbose)
  2006.         {
  2007.           printf_filtered ("done.\n");
  2008.           fflush (stdout);
  2009.         }
  2010.         }
  2011.     }
  2012.     }
  2013. }
  2014.  
  2015. /*
  2016.  
  2017. LOCAL FUNCTION
  2018.  
  2019.     init_psymbol_list -- initialize storage for partial symbols
  2020.  
  2021. SYNOPSIS
  2022.  
  2023.     static void init_psymbol_list (struct objfile *objfile, int total_symbols)
  2024.  
  2025. DESCRIPTION
  2026.  
  2027.     Initializes storage for all of the partial symbols that will be
  2028.     created by dwarf_build_psymtabs and subsidiaries.
  2029.  */
  2030.  
  2031. static void
  2032. init_psymbol_list (objfile, total_symbols)
  2033.      struct objfile *objfile;
  2034.      int total_symbols;
  2035. {
  2036.   /* Free any previously allocated psymbol lists.  */
  2037.   
  2038.   if (objfile -> global_psymbols.list)
  2039.     {
  2040.       mfree (objfile -> md, (PTR)objfile -> global_psymbols.list);
  2041.     }
  2042.   if (objfile -> static_psymbols.list)
  2043.     {
  2044.       mfree (objfile -> md, (PTR)objfile -> static_psymbols.list);
  2045.     }
  2046.   
  2047.   /* Current best guess is that there are approximately a twentieth
  2048.      of the total symbols (in a debugging file) are global or static
  2049.      oriented symbols */
  2050.   
  2051.   objfile -> global_psymbols.size = total_symbols / 10;
  2052.   objfile -> static_psymbols.size = total_symbols / 10;
  2053.   objfile -> global_psymbols.next =
  2054.     objfile -> global_psymbols.list = (struct partial_symbol *)
  2055.       xmmalloc (objfile -> md, objfile -> global_psymbols.size
  2056.                  * sizeof (struct partial_symbol));
  2057.   objfile -> static_psymbols.next =
  2058.     objfile -> static_psymbols.list = (struct partial_symbol *)
  2059.       xmmalloc (objfile -> md, objfile -> static_psymbols.size
  2060.                  * sizeof (struct partial_symbol));
  2061. }
  2062.  
  2063. /*
  2064.  
  2065. LOCAL FUNCTION
  2066.  
  2067.     add_enum_psymbol -- add enumeration members to partial symbol table
  2068.  
  2069. DESCRIPTION
  2070.  
  2071.     Given pointer to a DIE that is known to be for an enumeration,
  2072.     extract the symbolic names of the enumeration members and add
  2073.     partial symbols for them.
  2074. */
  2075.  
  2076. static void
  2077. add_enum_psymbol (dip, objfile)
  2078.      struct dieinfo *dip;
  2079.      struct objfile *objfile;
  2080. {
  2081.   char *scan;
  2082.   char *listend;
  2083.   long ltemp;
  2084.   short stemp;
  2085.   
  2086.   if ((scan = dip -> at_element_list) != NULL)
  2087.     {
  2088.       if (dip -> short_element_list)
  2089.     {
  2090.       (void) memcpy (&stemp, scan, sizeof (stemp));
  2091.       listend = scan + stemp + sizeof (stemp);
  2092.       scan += sizeof (stemp);
  2093.     }
  2094.       else
  2095.     {
  2096.       (void) memcpy (<emp, scan, sizeof (ltemp));
  2097.       listend = scan + ltemp + sizeof (ltemp);
  2098.       scan += sizeof (ltemp);
  2099.     }
  2100.       while (scan < listend)
  2101.     {
  2102.       scan += sizeof (long);
  2103.       ADD_PSYMBOL_TO_LIST (scan, strlen (scan), VAR_NAMESPACE, LOC_CONST,
  2104.                    objfile -> static_psymbols, 0);
  2105.       scan += strlen (scan) + 1;
  2106.     }
  2107.     }
  2108. }
  2109.  
  2110. /*
  2111.  
  2112. LOCAL FUNCTION
  2113.  
  2114.     add_partial_symbol -- add symbol to partial symbol table
  2115.  
  2116. DESCRIPTION
  2117.  
  2118.     Given a DIE, if it is one of the types that we want to
  2119.     add to a partial symbol table, finish filling in the die info
  2120.     and then add a partial symbol table entry for it.
  2121.  
  2122. */
  2123.  
  2124. static void
  2125. add_partial_symbol (dip, objfile)
  2126.      struct dieinfo *dip;
  2127.      struct objfile *objfile;
  2128. {
  2129.   switch (dip -> dietag)
  2130.     {
  2131.     case TAG_global_subroutine:
  2132.       record_minimal_symbol (dip -> at_name, dip -> at_low_pc, mst_text,
  2133.                 objfile);
  2134.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2135.                VAR_NAMESPACE, LOC_BLOCK,
  2136.                objfile -> global_psymbols,
  2137.                dip -> at_low_pc);
  2138.       break;
  2139.     case TAG_global_variable:
  2140.       record_minimal_symbol (dip -> at_name, locval (dip -> at_location),
  2141.                 mst_data, objfile);
  2142.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2143.                VAR_NAMESPACE, LOC_STATIC,
  2144.                objfile -> global_psymbols,
  2145.                0);
  2146.       break;
  2147.     case TAG_subroutine:
  2148.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2149.                VAR_NAMESPACE, LOC_BLOCK,
  2150.                objfile -> static_psymbols,
  2151.                dip -> at_low_pc);
  2152.       break;
  2153.     case TAG_local_variable:
  2154.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2155.                VAR_NAMESPACE, LOC_STATIC,
  2156.                objfile -> static_psymbols,
  2157.                0);
  2158.       break;
  2159.     case TAG_typedef:
  2160.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2161.                VAR_NAMESPACE, LOC_TYPEDEF,
  2162.                objfile -> static_psymbols,
  2163.                0);
  2164.       break;
  2165.     case TAG_structure_type:
  2166.     case TAG_union_type:
  2167.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2168.                STRUCT_NAMESPACE, LOC_TYPEDEF,
  2169.                objfile -> static_psymbols,
  2170.                0);
  2171.       break;
  2172.     case TAG_enumeration_type:
  2173.       if (dip -> at_name)
  2174.     {
  2175.       ADD_PSYMBOL_TO_LIST (dip -> at_name, strlen (dip -> at_name),
  2176.                    STRUCT_NAMESPACE, LOC_TYPEDEF,
  2177.                    objfile -> static_psymbols,
  2178.                    0);
  2179.     }
  2180.       add_enum_psymbol (dip, objfile);
  2181.       break;
  2182.     }
  2183. }
  2184.  
  2185. /*
  2186.  
  2187. LOCAL FUNCTION
  2188.  
  2189.     scan_partial_symbols -- scan DIE's within a single compilation unit
  2190.  
  2191. DESCRIPTION
  2192.  
  2193.     Process the DIE's within a single compilation unit, looking for
  2194.     interesting DIE's that contribute to the partial symbol table entry
  2195.     for this compilation unit.  Since we cannot follow any sibling
  2196.     chains without reading the complete DIE info for every DIE,
  2197.     it is probably faster to just sequentially check each one to
  2198.     see if it is one of the types we are interested in, and if so,
  2199.     then extract all the attributes info and generate a partial
  2200.     symbol table entry.
  2201.  
  2202. NOTES
  2203.  
  2204.     Don't attempt to add anonymous structures or unions since they have
  2205.     no name.  Anonymous enumerations however are processed, because we
  2206.     want to extract their member names (the check for a tag name is
  2207.     done later).
  2208.  
  2209.     Also, for variables and subroutines, check that this is the place
  2210.     where the actual definition occurs, rather than just a reference
  2211.     to an external.
  2212.  */
  2213.  
  2214. static void
  2215. scan_partial_symbols (thisdie, enddie, objfile)
  2216.      char *thisdie;
  2217.      char *enddie;
  2218.      struct objfile *objfile;
  2219. {
  2220.   char *nextdie;
  2221.   struct dieinfo di;
  2222.   
  2223.   while (thisdie < enddie)
  2224.     {
  2225.       basicdieinfo (&di, thisdie);
  2226.       if (di.dielength < sizeof (long))
  2227.     {
  2228.       break;
  2229.     }
  2230.       else
  2231.     {
  2232.       nextdie = thisdie + di.dielength;
  2233.       /* To avoid getting complete die information for every die, we
  2234.          only do it (below) for the cases we are interested in. */
  2235.       switch (di.dietag)
  2236.         {
  2237.         case TAG_global_subroutine:
  2238.         case TAG_subroutine:
  2239.         case TAG_global_variable:
  2240.         case TAG_local_variable:
  2241.           completedieinfo (&di);
  2242.           if (di.at_name && (di.has_at_low_pc || di.at_location))
  2243.         {
  2244.           add_partial_symbol (&di, objfile);
  2245.         }
  2246.           break;
  2247.         case TAG_typedef:
  2248.         case TAG_structure_type:
  2249.         case TAG_union_type:
  2250.           completedieinfo (&di);
  2251.           if (di.at_name)
  2252.         {
  2253.           add_partial_symbol (&di, objfile);
  2254.         }
  2255.           break;
  2256.         case TAG_enumeration_type:
  2257.           completedieinfo (&di);
  2258.           add_partial_symbol (&di, objfile);
  2259.           break;
  2260.         }
  2261.     }
  2262.       thisdie = nextdie;
  2263.     }
  2264. }
  2265.  
  2266. /*
  2267.  
  2268. LOCAL FUNCTION
  2269.  
  2270.     scan_compilation_units -- build a psymtab entry for each compilation
  2271.  
  2272. DESCRIPTION
  2273.  
  2274.     This is the top level dwarf parsing routine for building partial
  2275.     symbol tables.
  2276.  
  2277.     It scans from the beginning of the DWARF table looking for the first
  2278.     TAG_compile_unit DIE, and then follows the sibling chain to locate
  2279.     each additional TAG_compile_unit DIE.
  2280.    
  2281.     For each TAG_compile_unit DIE it creates a partial symtab structure,
  2282.     calls a subordinate routine to collect all the compilation unit's
  2283.     global DIE's, file scope DIEs, typedef DIEs, etc, and then links the
  2284.     new partial symtab structure into the partial symbol table.  It also
  2285.     records the appropriate information in the partial symbol table entry
  2286.     to allow the chunk of DIE's and line number table for this compilation
  2287.     unit to be located and re-read later, to generate a complete symbol
  2288.     table entry for the compilation unit.
  2289.  
  2290.     Thus it effectively partitions up a chunk of DIE's for multiple
  2291.     compilation units into smaller DIE chunks and line number tables,
  2292.     and associates them with a partial symbol table entry.
  2293.  
  2294. NOTES
  2295.  
  2296.     If any compilation unit has no line number table associated with
  2297.     it for some reason (a missing at_stmt_list attribute, rather than
  2298.     just one with a value of zero, which is valid) then we ensure that
  2299.     the recorded file offset is zero so that the routine which later
  2300.     reads line number table fragments knows that there is no fragment
  2301.     to read.
  2302.  
  2303. RETURNS
  2304.  
  2305.     Returns no value.
  2306.  
  2307.  */
  2308.  
  2309. static void
  2310. scan_compilation_units (filename, thisdie, enddie, dbfoff, lnoffset, objfile)
  2311.      char *filename;
  2312.      char *thisdie;
  2313.      char *enddie;
  2314.      unsigned int dbfoff;
  2315.      unsigned int lnoffset;
  2316.      struct objfile *objfile;
  2317. {
  2318.   char *nextdie;
  2319.   struct dieinfo di;
  2320.   struct partial_symtab *pst;
  2321.   int culength;
  2322.   int curoff;
  2323.   int curlnoffset;
  2324.  
  2325.   while (thisdie < enddie)
  2326.     {
  2327.       basicdieinfo (&di, thisdie);
  2328.       if (di.dielength < sizeof (long))
  2329.     {
  2330.       break;
  2331.     }
  2332.       else if (di.dietag != TAG_compile_unit)
  2333.     {
  2334.       nextdie = thisdie + di.dielength;
  2335.     }
  2336.       else
  2337.     {
  2338.       completedieinfo (&di);
  2339.       if (di.at_sibling != 0)
  2340.         {
  2341.           nextdie = dbbase + di.at_sibling - dbroff;
  2342.         }
  2343.       else
  2344.         {
  2345.           nextdie = thisdie + di.dielength;
  2346.         }
  2347.       curoff = thisdie - dbbase;
  2348.       culength = nextdie - thisdie;
  2349.       curlnoffset = di.has_at_stmt_list ? lnoffset + di.at_stmt_list : 0;
  2350.  
  2351.       /* First allocate a new partial symbol table structure */
  2352.  
  2353.       pst = start_psymtab_common (objfile, baseaddr, di.at_name,
  2354.                       di.at_low_pc,
  2355.                       objfile -> global_psymbols.next,
  2356.                       objfile -> static_psymbols.next);
  2357.  
  2358.       pst -> texthigh = di.at_high_pc;
  2359.       pst -> read_symtab_private = (char *)
  2360.           obstack_alloc (&objfile -> psymbol_obstack,
  2361.                  sizeof (struct dwfinfo));
  2362.       DBFOFF (pst) = dbfoff;
  2363.       DBROFF (pst) = curoff;
  2364.       DBLENGTH (pst) = culength;
  2365.       LNFOFF (pst)  = curlnoffset;
  2366.       pst -> read_symtab = dwarf_psymtab_to_symtab;
  2367.  
  2368.       /* Now look for partial symbols */
  2369.  
  2370.       scan_partial_symbols (thisdie + di.dielength, nextdie, objfile);
  2371.  
  2372.       pst -> n_global_syms = objfile -> global_psymbols.next -
  2373.         (objfile -> global_psymbols.list + pst -> globals_offset);
  2374.       pst -> n_static_syms = objfile -> static_psymbols.next - 
  2375.         (objfile -> static_psymbols.list + pst -> statics_offset);
  2376.       sort_pst_symbols (pst);
  2377.       /* If there is already a psymtab or symtab for a file of this name,
  2378.          remove it. (If there is a symtab, more drastic things also
  2379.          happen.)  This happens in VxWorks.  */
  2380.       free_named_symtabs (pst -> filename);
  2381.     }
  2382.       thisdie = nextdie;      
  2383.     }
  2384. }
  2385.  
  2386. /*
  2387.  
  2388. LOCAL FUNCTION
  2389.  
  2390.     new_symbol -- make a symbol table entry for a new symbol
  2391.  
  2392. SYNOPSIS
  2393.  
  2394.     static struct symbol *new_symbol (struct dieinfo *dip,
  2395.                       struct objfile *objfile)
  2396.  
  2397. DESCRIPTION
  2398.  
  2399.     Given a pointer to a DWARF information entry, figure out if we need
  2400.     to make a symbol table entry for it, and if so, create a new entry
  2401.     and return a pointer to it.
  2402.  */
  2403.  
  2404. static struct symbol *
  2405. new_symbol (dip, objfile)
  2406.      struct dieinfo *dip;
  2407.      struct objfile *objfile;
  2408. {
  2409.   struct symbol *sym = NULL;
  2410.   
  2411.   if (dip -> at_name != NULL)
  2412.     {
  2413.       sym = (struct symbol *) obstack_alloc (&objfile -> symbol_obstack,
  2414.                          sizeof (struct symbol));
  2415.       (void) memset (sym, 0, sizeof (struct symbol));
  2416.       SYMBOL_NAME (sym) = create_name (dip -> at_name, &objfile->symbol_obstack);
  2417.       /* default assumptions */
  2418.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  2419.       SYMBOL_CLASS (sym) = LOC_STATIC;
  2420.       SYMBOL_TYPE (sym) = decode_die_type (dip);
  2421.       switch (dip -> dietag)
  2422.     {
  2423.     case TAG_label:
  2424.       SYMBOL_VALUE (sym) = dip -> at_low_pc;
  2425.       SYMBOL_CLASS (sym) = LOC_LABEL;
  2426.       break;
  2427.     case TAG_global_subroutine:
  2428.     case TAG_subroutine:
  2429.       SYMBOL_VALUE (sym) = dip -> at_low_pc;
  2430.       SYMBOL_TYPE (sym) = lookup_function_type (SYMBOL_TYPE (sym));
  2431.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  2432.       if (dip -> dietag == TAG_global_subroutine)
  2433.         {
  2434.           add_symbol_to_list (sym, &global_symbols);
  2435.         }
  2436.       else
  2437.         {
  2438.           add_symbol_to_list (sym, list_in_scope);
  2439.         }
  2440.       break;
  2441.     case TAG_global_variable:
  2442.       if (dip -> at_location != NULL)
  2443.         {
  2444.           SYMBOL_VALUE (sym) = locval (dip -> at_location);
  2445.           add_symbol_to_list (sym, &global_symbols);
  2446.           SYMBOL_CLASS (sym) = LOC_STATIC;
  2447.           SYMBOL_VALUE (sym) += baseaddr;
  2448.         }
  2449.       break;
  2450.     case TAG_local_variable:
  2451.       if (dip -> at_location != NULL)
  2452.         {
  2453.           SYMBOL_VALUE (sym) = locval (dip -> at_location);
  2454.           add_symbol_to_list (sym, list_in_scope);
  2455.           if (isreg)
  2456.         {
  2457.           SYMBOL_CLASS (sym) = LOC_REGISTER;
  2458.         }
  2459.           else if (offreg)
  2460.         {
  2461.           SYMBOL_CLASS (sym) = LOC_LOCAL;
  2462.         }
  2463.           else
  2464.         {
  2465.           SYMBOL_CLASS (sym) = LOC_STATIC;
  2466.           SYMBOL_VALUE (sym) += baseaddr;
  2467.         }
  2468.         }
  2469.       break;
  2470.     case TAG_formal_parameter:
  2471.       if (dip -> at_location != NULL)
  2472.         {
  2473.           SYMBOL_VALUE (sym) = locval (dip -> at_location);
  2474.         }
  2475.       add_symbol_to_list (sym, list_in_scope);
  2476.       if (isreg)
  2477.         {
  2478.           SYMBOL_CLASS (sym) = LOC_REGPARM;
  2479.         }
  2480.       else
  2481.         {
  2482.           SYMBOL_CLASS (sym) = LOC_ARG;
  2483.         }
  2484.       break;
  2485.     case TAG_unspecified_parameters:
  2486.       /* From varargs functions; gdb doesn't seem to have any interest in
  2487.          this information, so just ignore it for now. (FIXME?) */
  2488.       break;
  2489.     case TAG_structure_type:
  2490.     case TAG_union_type:
  2491.     case TAG_enumeration_type:
  2492.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  2493.       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  2494.       add_symbol_to_list (sym, list_in_scope);
  2495.       break;
  2496.     case TAG_typedef:
  2497.       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  2498.       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  2499.       add_symbol_to_list (sym, list_in_scope);
  2500.       break;
  2501.     default:
  2502.       /* Not a tag we recognize.  Hopefully we aren't processing trash
  2503.          data, but since we must specifically ignore things we don't
  2504.          recognize, there is nothing else we should do at this point. */
  2505.       break;
  2506.     }
  2507.     }
  2508.   return (sym);
  2509. }
  2510.  
  2511. /*
  2512.  
  2513. LOCAL FUNCTION
  2514.  
  2515.     decode_mod_fund_type -- decode a modified fundamental type
  2516.  
  2517. SYNOPSIS
  2518.  
  2519.     static struct type *decode_mod_fund_type (char *typedata)
  2520.  
  2521. DESCRIPTION
  2522.  
  2523.     Decode a block of data containing a modified fundamental
  2524.     type specification.  TYPEDATA is a pointer to the block,
  2525.     which consists of a two byte length, containing the size
  2526.     of the rest of the block.  At the end of the block is a
  2527.     two byte value that gives the fundamental type.  Everything
  2528.     in between are type modifiers.
  2529.  
  2530.     We simply compute the number of modifiers and call the general
  2531.     function decode_modified_type to do the actual work.
  2532. */
  2533.  
  2534. static struct type *
  2535. decode_mod_fund_type (typedata)
  2536.      char *typedata;
  2537. {
  2538.   struct type *typep = NULL;
  2539.   unsigned short modcount;
  2540.   unsigned char *modifiers;
  2541.   
  2542.   /* Get the total size of the block, exclusive of the size itself */
  2543.   (void) memcpy (&modcount, typedata, sizeof (short));
  2544.   /* Deduct the size of the fundamental type bytes at the end of the block. */
  2545.   modcount -= sizeof (short);
  2546.   /* Skip over the two size bytes at the beginning of the block. */
  2547.   modifiers = (unsigned char *) typedata + sizeof (short);
  2548.   /* Now do the actual decoding */
  2549.   typep = decode_modified_type (modifiers, modcount, AT_mod_fund_type);
  2550.   return (typep);
  2551. }
  2552.  
  2553. /*
  2554.  
  2555. LOCAL FUNCTION
  2556.  
  2557.     decode_mod_u_d_type -- decode a modified user defined type
  2558.  
  2559. SYNOPSIS
  2560.  
  2561.     static struct type *decode_mod_u_d_type (char *typedata)
  2562.  
  2563. DESCRIPTION
  2564.  
  2565.     Decode a block of data containing a modified user defined
  2566.     type specification.  TYPEDATA is a pointer to the block,
  2567.     which consists of a two byte length, containing the size
  2568.     of the rest of the block.  At the end of the block is a
  2569.     four byte value that gives a reference to a user defined type.
  2570.     Everything in between are type modifiers.
  2571.  
  2572.     We simply compute the number of modifiers and call the general
  2573.     function decode_modified_type to do the actual work.
  2574. */
  2575.  
  2576. static struct type *
  2577. decode_mod_u_d_type (typedata)
  2578.      char *typedata;
  2579. {
  2580.   struct type *typep = NULL;
  2581.   unsigned short modcount;
  2582.   unsigned char *modifiers;
  2583.   
  2584.   /* Get the total size of the block, exclusive of the size itself */
  2585.   (void) memcpy (&modcount, typedata, sizeof (short));
  2586.   /* Deduct the size of the reference type bytes at the end of the block. */
  2587.   modcount -= sizeof (long);
  2588.   /* Skip over the two size bytes at the beginning of the block. */
  2589.   modifiers = (unsigned char *) typedata + sizeof (short);
  2590.   /* Now do the actual decoding */
  2591.   typep = decode_modified_type (modifiers, modcount, AT_mod_u_d_type);
  2592.   return (typep);
  2593. }
  2594.  
  2595. /*
  2596.  
  2597. LOCAL FUNCTION
  2598.  
  2599.     decode_modified_type -- decode modified user or fundamental type
  2600.  
  2601. SYNOPSIS
  2602.  
  2603.     static struct type *decode_modified_type (unsigned char *modifiers,
  2604.         unsigned short modcount, int mtype)
  2605.  
  2606. DESCRIPTION
  2607.  
  2608.     Decode a modified type, either a modified fundamental type or
  2609.     a modified user defined type.  MODIFIERS is a pointer to the
  2610.     block of bytes that define MODCOUNT modifiers.  Immediately
  2611.     following the last modifier is a short containing the fundamental
  2612.     type or a long containing the reference to the user defined
  2613.     type.  Which one is determined by MTYPE, which is either
  2614.     AT_mod_fund_type or AT_mod_u_d_type to indicate what modified
  2615.     type we are generating.
  2616.  
  2617.     We call ourself recursively to generate each modified type,`
  2618.     until MODCOUNT reaches zero, at which point we have consumed
  2619.     all the modifiers and generate either the fundamental type or
  2620.     user defined type.  When the recursion unwinds, each modifier
  2621.     is applied in turn to generate the full modified type.
  2622.  
  2623. NOTES
  2624.  
  2625.     If we find a modifier that we don't recognize, and it is not one
  2626.     of those reserved for application specific use, then we issue a
  2627.     warning and simply ignore the modifier.
  2628.  
  2629. BUGS
  2630.  
  2631.     We currently ignore MOD_const and MOD_volatile.  (FIXME)
  2632.  
  2633.  */
  2634.  
  2635. static struct type *
  2636. decode_modified_type (modifiers, modcount, mtype)
  2637.      unsigned char *modifiers;
  2638.      unsigned int modcount;
  2639.      int mtype;
  2640. {
  2641.   struct type *typep = NULL;
  2642.   unsigned short fundtype;
  2643.   DIEREF dieref;
  2644.   unsigned char modifier;
  2645.   
  2646.   if (modcount == 0)
  2647.     {
  2648.       switch (mtype)
  2649.     {
  2650.     case AT_mod_fund_type:
  2651.       (void) memcpy (&fundtype, modifiers, sizeof (short));
  2652.       typep = decode_fund_type (fundtype);
  2653.       break;
  2654.     case AT_mod_u_d_type:
  2655.       (void) memcpy (&dieref, modifiers, sizeof (DIEREF));
  2656.       if ((typep = lookup_utype (dieref)) == NULL)
  2657.         {
  2658.           typep = alloc_utype (dieref, NULL);
  2659.         }
  2660.       break;
  2661.     default:
  2662.       SQUAWK (("botched modified type decoding (mtype 0x%x)", mtype));
  2663.       typep = lookup_fundamental_type (current_objfile, FT_INTEGER);
  2664.       break;
  2665.     }
  2666.     }
  2667.   else
  2668.     {
  2669.       modifier = *modifiers++;
  2670.       typep = decode_modified_type (modifiers, --modcount, mtype);
  2671.       switch (modifier)
  2672.     {
  2673.     case MOD_pointer_to:
  2674.       typep = lookup_pointer_type (typep);
  2675.       break;
  2676.     case MOD_reference_to:
  2677.       typep = lookup_reference_type (typep);
  2678.       break;
  2679.     case MOD_const:
  2680.       SQUAWK (("type modifier 'const' ignored"));    /* FIXME */
  2681.       break;
  2682.     case MOD_volatile:
  2683.       SQUAWK (("type modifier 'volatile' ignored"));    /* FIXME */
  2684.       break;
  2685.     default:
  2686.       if (!(MOD_lo_user <= modifier && modifier <= MOD_hi_user))
  2687.         {
  2688.           SQUAWK (("unknown type modifier %u", modifier));
  2689.         }
  2690.       break;
  2691.     }
  2692.     }
  2693.   return (typep);
  2694. }
  2695.  
  2696. /*
  2697.  
  2698. LOCAL FUNCTION
  2699.  
  2700.     decode_fund_type -- translate basic DWARF type to gdb base type
  2701.  
  2702. DESCRIPTION
  2703.  
  2704.     Given an integer that is one of the fundamental DWARF types,
  2705.     translate it to one of the basic internal gdb types and return
  2706.     a pointer to the appropriate gdb type (a "struct type *").
  2707.  
  2708. NOTES
  2709.  
  2710.     If we encounter a fundamental type that we are unprepared to
  2711.     deal with, and it is not in the range of those types defined
  2712.     as application specific types, then we issue a warning and
  2713.     treat the type as an "int".
  2714. */
  2715.  
  2716. static struct type *
  2717. decode_fund_type (fundtype)
  2718.      unsigned int fundtype;
  2719. {
  2720.   struct type *typep = NULL;
  2721.   
  2722.   switch (fundtype)
  2723.     {
  2724.  
  2725.     case FT_void:
  2726.       typep = lookup_fundamental_type (current_objfile, FT_VOID);
  2727.       break;
  2728.     
  2729.     case FT_boolean:        /* Was FT_set in AT&T version */
  2730.       typep = lookup_fundamental_type (current_objfile, FT_BOOLEAN);
  2731.       break;
  2732.  
  2733.     case FT_pointer:        /* (void *) */
  2734.       typep = lookup_fundamental_type (current_objfile, FT_VOID);
  2735.       typep = lookup_pointer_type (typep);
  2736.       break;
  2737.     
  2738.     case FT_char:
  2739.       typep = lookup_fundamental_type (current_objfile, FT_CHAR);
  2740.       break;
  2741.     
  2742.     case FT_signed_char:
  2743.       typep = lookup_fundamental_type (current_objfile, FT_SIGNED_CHAR);
  2744.       break;
  2745.  
  2746.     case FT_unsigned_char:
  2747.       typep = lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
  2748.       break;
  2749.     
  2750.     case FT_short:
  2751.       typep = lookup_fundamental_type (current_objfile, FT_SHORT);
  2752.       break;
  2753.  
  2754.     case FT_signed_short:
  2755.       typep = lookup_fundamental_type (current_objfile, FT_SIGNED_SHORT);
  2756.       break;
  2757.     
  2758.     case FT_unsigned_short:
  2759.       typep = lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
  2760.       break;
  2761.     
  2762.     case FT_integer:
  2763.       typep = lookup_fundamental_type (current_objfile, FT_INTEGER);
  2764.       break;
  2765.  
  2766.     case FT_signed_integer:
  2767.       typep = lookup_fundamental_type (current_objfile, FT_SIGNED_INTEGER);
  2768.       break;
  2769.     
  2770.     case FT_unsigned_integer:
  2771.       typep = lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
  2772.       break;
  2773.     
  2774.     case FT_long:
  2775.       typep = lookup_fundamental_type (current_objfile, FT_LONG);
  2776.       break;
  2777.  
  2778.     case FT_signed_long:
  2779.       typep = lookup_fundamental_type (current_objfile, FT_SIGNED_LONG);
  2780.       break;
  2781.     
  2782.     case FT_unsigned_long:
  2783.       typep = lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
  2784.       break;
  2785.     
  2786.     case FT_long_long:
  2787.       typep = lookup_fundamental_type (current_objfile, FT_LONG_LONG);
  2788.       break;
  2789.  
  2790.     case FT_signed_long_long:
  2791.       typep = lookup_fundamental_type (current_objfile, FT_SIGNED_LONG_LONG);
  2792.       break;
  2793.  
  2794.     case FT_unsigned_long_long:
  2795.       typep = lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG_LONG);
  2796.       break;
  2797.  
  2798.     case FT_float:
  2799.       typep = lookup_fundamental_type (current_objfile, FT_FLOAT);
  2800.       break;
  2801.     
  2802.     case FT_dbl_prec_float:
  2803.       typep = lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
  2804.       break;
  2805.     
  2806.     case FT_ext_prec_float:
  2807.       typep = lookup_fundamental_type (current_objfile, FT_EXT_PREC_FLOAT);
  2808.       break;
  2809.     
  2810.     case FT_complex:
  2811.       typep = lookup_fundamental_type (current_objfile, FT_COMPLEX);
  2812.       break;
  2813.     
  2814.     case FT_dbl_prec_complex:
  2815.       typep = lookup_fundamental_type (current_objfile, FT_DBL_PREC_COMPLEX);
  2816.       break;
  2817.     
  2818.     case FT_ext_prec_complex:
  2819.       typep = lookup_fundamental_type (current_objfile, FT_EXT_PREC_COMPLEX);
  2820.       break;
  2821.     
  2822.     }
  2823.  
  2824.   if ((typep == NULL) && !(FT_lo_user <= fundtype && fundtype <= FT_hi_user))
  2825.     {
  2826.       SQUAWK (("unexpected fundamental type 0x%x", fundtype));
  2827.       typep = lookup_fundamental_type (current_objfile, FT_VOID);
  2828.     }
  2829.     
  2830.   return (typep);
  2831. }
  2832.  
  2833. /*
  2834.  
  2835. LOCAL FUNCTION
  2836.  
  2837.     create_name -- allocate a fresh copy of a string on an obstack
  2838.  
  2839. DESCRIPTION
  2840.  
  2841.     Given a pointer to a string and a pointer to an obstack, allocates
  2842.     a fresh copy of the string on the specified obstack.
  2843.  
  2844. */
  2845.  
  2846. static char *
  2847. create_name (name, obstackp)
  2848.      char *name;
  2849.      struct obstack *obstackp;
  2850. {
  2851.   int length;
  2852.   char *newname;
  2853.  
  2854.   length = strlen (name) + 1;
  2855.   newname = (char *) obstack_alloc (obstackp, length);
  2856.   (void) strcpy (newname, name);
  2857.   return (newname);
  2858. }
  2859.  
  2860. /*
  2861.  
  2862. LOCAL FUNCTION
  2863.  
  2864.     basicdieinfo -- extract the minimal die info from raw die data
  2865.  
  2866. SYNOPSIS
  2867.  
  2868.     void basicdieinfo (char *diep, struct dieinfo *dip)
  2869.  
  2870. DESCRIPTION
  2871.  
  2872.     Given a pointer to raw DIE data, and a pointer to an instance of a
  2873.     die info structure, this function extracts the basic information
  2874.     from the DIE data required to continue processing this DIE, along
  2875.     with some bookkeeping information about the DIE.
  2876.  
  2877.     The information we absolutely must have includes the DIE tag,
  2878.     and the DIE length.  If we need the sibling reference, then we
  2879.     will have to call completedieinfo() to process all the remaining
  2880.     DIE information.
  2881.  
  2882.     Note that since there is no guarantee that the data is properly
  2883.     aligned in memory for the type of access required (indirection
  2884.     through anything other than a char pointer), we use memcpy to
  2885.     shuffle data items larger than a char.  Possibly inefficient, but
  2886.     quite portable.
  2887.  
  2888.     We also take care of some other basic things at this point, such
  2889.     as ensuring that the instance of the die info structure starts
  2890.     out completely zero'd and that curdie is initialized for use
  2891.     in error reporting if we have a problem with the current die.
  2892.  
  2893. NOTES
  2894.  
  2895.     All DIE's must have at least a valid length, thus the minimum
  2896.     DIE size is sizeof (long).  In order to have a valid tag, the
  2897.     DIE size must be at least sizeof (short) larger, otherwise they
  2898.     are forced to be TAG_padding DIES.
  2899.  
  2900.     Padding DIES must be at least sizeof(long) in length, implying that
  2901.     if a padding DIE is used for alignment and the amount needed is less
  2902.     than sizeof(long) then the padding DIE has to be big enough to align
  2903.     to the next alignment boundry.
  2904.  */
  2905.  
  2906. static void
  2907. basicdieinfo (dip, diep)
  2908.      struct dieinfo *dip;
  2909.      char *diep;
  2910. {
  2911.   curdie = dip;
  2912.   (void) memset (dip, 0, sizeof (struct dieinfo));
  2913.   dip -> die = diep;
  2914.   dip -> dieref = dbroff + (diep - dbbase);
  2915.   (void) memcpy (&dip -> dielength, diep, sizeof (long));
  2916.   if (dip -> dielength < sizeof (long))
  2917.     {
  2918.       dwarfwarn ("malformed DIE, bad length (%d bytes)", dip -> dielength);
  2919.     }
  2920.   else if (dip -> dielength < (sizeof (long) + sizeof (short)))
  2921.     {
  2922.       dip -> dietag = TAG_padding;
  2923.     }
  2924.   else
  2925.     {
  2926.       (void) memcpy (&dip -> dietag, diep + sizeof (long), sizeof (short));
  2927.     }
  2928. }
  2929.  
  2930. /*
  2931.  
  2932. LOCAL FUNCTION
  2933.  
  2934.     completedieinfo -- finish reading the information for a given DIE
  2935.  
  2936. SYNOPSIS
  2937.  
  2938.     void completedieinfo (struct dieinfo *dip)
  2939.  
  2940. DESCRIPTION
  2941.  
  2942.     Given a pointer to an already partially initialized die info structure,
  2943.     scan the raw DIE data and finish filling in the die info structure
  2944.     from the various attributes found.
  2945.    
  2946.     Note that since there is no guarantee that the data is properly
  2947.     aligned in memory for the type of access required (indirection
  2948.     through anything other than a char pointer), we use memcpy to
  2949.     shuffle data items larger than a char.  Possibly inefficient, but
  2950.     quite portable.
  2951.  
  2952. NOTES
  2953.  
  2954.     Each time we are called, we increment the diecount variable, which
  2955.     keeps an approximate count of the number of dies processed for
  2956.     each compilation unit.  This information is presented to the user
  2957.     if the info_verbose flag is set.
  2958.  
  2959.  */
  2960.  
  2961. static void
  2962. completedieinfo (dip)
  2963.      struct dieinfo *dip;
  2964. {
  2965.   char *diep;            /* Current pointer into raw DIE data */
  2966.   char *end;            /* Terminate DIE scan here */
  2967.   unsigned short attr;        /* Current attribute being scanned */
  2968.   unsigned short form;        /* Form of the attribute */
  2969.   short block2sz;        /* Size of a block2 attribute field */
  2970.   long block4sz;        /* Size of a block4 attribute field */
  2971.   
  2972.   diecount++;
  2973.   diep = dip -> die;
  2974.   end = diep + dip -> dielength;
  2975.   diep += sizeof (long) + sizeof (short);
  2976.   while (diep < end)
  2977.     {
  2978.       (void) memcpy (&attr, diep, sizeof (short));
  2979.       diep += sizeof (short);
  2980.       switch (attr)
  2981.     {
  2982.     case AT_fund_type:
  2983.       (void) memcpy (&dip -> at_fund_type, diep, sizeof (short));
  2984.       break;
  2985.     case AT_ordering:
  2986.       (void) memcpy (&dip -> at_ordering, diep, sizeof (short));
  2987.       break;
  2988.     case AT_bit_offset:
  2989.       (void) memcpy (&dip -> at_bit_offset, diep, sizeof (short));
  2990.       break;
  2991.     case AT_visibility:
  2992.       (void) memcpy (&dip -> at_visibility, diep, sizeof (short));
  2993.       break;
  2994.     case AT_sibling:
  2995.       (void) memcpy (&dip -> at_sibling, diep, sizeof (long));
  2996.       break;
  2997.     case AT_stmt_list:
  2998.       (void) memcpy (&dip -> at_stmt_list, diep, sizeof (long));
  2999.       dip -> has_at_stmt_list = 1;
  3000.       break;
  3001.     case AT_low_pc:
  3002.       (void) memcpy (&dip -> at_low_pc, diep, sizeof (long));
  3003.       dip -> at_low_pc += baseaddr;
  3004.       dip -> has_at_low_pc = 1;
  3005.       break;
  3006.     case AT_high_pc:
  3007.       (void) memcpy (&dip -> at_high_pc, diep, sizeof (long));
  3008.       dip -> at_high_pc += baseaddr;
  3009.       break;
  3010.     case AT_language:
  3011.       (void) memcpy (&dip -> at_language, diep, sizeof (long));
  3012.       break;
  3013.     case AT_user_def_type:
  3014.       (void) memcpy (&dip -> at_user_def_type, diep, sizeof (long));
  3015.       break;
  3016.     case AT_byte_size:
  3017.       (void) memcpy (&dip -> at_byte_size, diep, sizeof (long));
  3018.       break;
  3019.     case AT_bit_size:
  3020.       (void) memcpy (&dip -> at_bit_size, diep, sizeof (long));
  3021.       break;
  3022.     case AT_member:
  3023.       (void) memcpy (&dip -> at_member, diep, sizeof (long));
  3024.       break;
  3025.     case AT_discr:
  3026.       (void) memcpy (&dip -> at_discr, diep, sizeof (long));
  3027.       break;
  3028.     case AT_import:
  3029.       (void) memcpy (&dip -> at_import, diep, sizeof (long));
  3030.       break;
  3031.     case AT_location:
  3032.       dip -> at_location = diep;
  3033.       break;
  3034.     case AT_mod_fund_type:
  3035.       dip -> at_mod_fund_type = diep;
  3036.       break;
  3037.     case AT_subscr_data:
  3038.       dip -> at_subscr_data = diep;
  3039.       break;
  3040.     case AT_mod_u_d_type:
  3041.       dip -> at_mod_u_d_type = diep;
  3042.       break;
  3043.     case AT_element_list:
  3044.       dip -> at_element_list = diep;
  3045.       dip -> short_element_list = 0;
  3046.       break;
  3047.     case AT_short_element_list:
  3048.       dip -> at_element_list = diep;
  3049.       dip -> short_element_list = 1;
  3050.       break;
  3051.     case AT_discr_value:
  3052.       dip -> at_discr_value = diep;
  3053.       break;
  3054.     case AT_string_length:
  3055.       dip -> at_string_length = diep;
  3056.       break;
  3057.     case AT_name:
  3058.       dip -> at_name = diep;
  3059.       break;
  3060.     case AT_comp_dir:
  3061.       dip -> at_comp_dir = diep;
  3062.       break;
  3063.     case AT_producer:
  3064.       dip -> at_producer = diep;
  3065.       break;
  3066.     case AT_frame_base:
  3067.       (void) memcpy (&dip -> at_frame_base, diep, sizeof (long));
  3068.       break;
  3069.     case AT_start_scope:
  3070.       (void) memcpy (&dip -> at_start_scope, diep, sizeof (long));
  3071.       break;
  3072.     case AT_stride_size:
  3073.       (void) memcpy (&dip -> at_stride_size, diep, sizeof (long));
  3074.       break;
  3075.     case AT_src_info:
  3076.       (void) memcpy (&dip -> at_src_info, diep, sizeof (long));
  3077.       break;
  3078.     case AT_prototyped:
  3079.       (void) memcpy (&dip -> at_prototyped, diep, sizeof (short));
  3080.       break;
  3081.     default:
  3082.       /* Found an attribute that we are unprepared to handle.  However
  3083.          it is specifically one of the design goals of DWARF that
  3084.          consumers should ignore unknown attributes.  As long as the
  3085.          form is one that we recognize (so we know how to skip it),
  3086.          we can just ignore the unknown attribute. */
  3087.       break;
  3088.     }
  3089.       form = attr & 0xF;
  3090.       switch (form)
  3091.     {
  3092.     case FORM_DATA2:
  3093.       diep += sizeof (short);
  3094.       break;
  3095.     case FORM_DATA4:
  3096.       diep += sizeof (long);
  3097.       break;
  3098.     case FORM_DATA8:
  3099.       diep += 8 * sizeof (char);    /* sizeof (long long) ? */
  3100.       break;
  3101.     case FORM_ADDR:
  3102.     case FORM_REF:
  3103.       diep += sizeof (long);
  3104.       break;
  3105.     case FORM_BLOCK2:
  3106.       (void) memcpy (&block2sz, diep, sizeof (short));
  3107.       block2sz += sizeof (short);
  3108.       diep += block2sz;
  3109.       break;
  3110.     case FORM_BLOCK4:
  3111.       (void) memcpy (&block4sz, diep, sizeof (long));
  3112.       block4sz += sizeof (long);
  3113.       diep += block4sz;
  3114.       break;
  3115.     case FORM_STRING:
  3116.       diep += strlen (diep) + 1;
  3117.       break;
  3118.     default:
  3119.       SQUAWK (("unknown attribute form (0x%x), skipped rest", form));
  3120.       diep = end;
  3121.       break;
  3122.     }
  3123.     }
  3124. }
  3125.